1
votes

The base model of my application has a status attribute. Let's assume, for simplicity, that status might be either pending or deleted.

I have an upper menu with these two status values, when you click one of them you see all objects with this status (I use router to trigger a filter).

My problem is that I need to draw a different template for each status.

  • deleted object has delete forever and recover buttons
  • pending object has delete, edit and some other buttons (also some textarea...)

I wonder what would be the best solution for this problem.
I thought of creating a different view for each status, but then I don't know how to deal with it in the collection level.
I also thought of creating different templates and deal with it in the model-view level, but again - I have no idea whether it is possible and if yes - how.

Finally, I can solve it with same template and view, hiding what is not necessary inside the view, but then the code becomes quite ugly in my point of view.

Ideas?? Thanks!

1

1 Answers

2
votes

If you want to create a different view for each status, you do it this way :

Router {

    clickDeletedMenu : {

        var collection = new MyCollection();
        var deletedView = new DeletedView({ model : collection });

        collection.fetch({ status : 'deleted' }); // filter deleted objects
    },

    clickPendingMenu : {

        var collection = new MyCollection();
        var pendingView = new PendingView({ model : collection });

        collection.fetch({ status : 'pending' }); // filter deleted objects
    },

}

If you want to create differents templates, you do it this way :

View {

    render : {

        if (this.model.status == 'deleted') {
            // render deleted template
        } else {
            // render pending template
        }
    }
}

Finally, in my point of view, you can use the same template and view, and hiding what is not necessary inside the template not the view.

nb : the code is used just to illustrate the idea, it's not going to execute :)