jaredrada
jaredrada

Reputation: 1150

backbone how to handle same model with multiple views

edit: can I pass a parameter to a new view declaration ? so something like

new articleView({
    template: "my desired template",
})

Suppose I have an array of objects, where each object represents a topic and contains a few properties: a title, a template type, and an array of articles. All the topics render nearly identical minus a few template differences.

I am using backbone and I have a general question: should each "topic" be a separate instance of the same collection type? Where would I declare the template type to be used for each topic? Should the collection have a variable template type property?

var topics = [
    {
        title: "Topic One",
        template: "detailedView",
        articles: [
            {
                 title: "A very good article",
                 timestamp: "2013-01-24"
            },
            {
                 //more articles here
            }
        ]
    },
    {
         //another topic here...
    }  

];

Upvotes: 1

Views: 119

Answers (1)

net.uk.sweet
net.uk.sweet

Reputation: 12431

To answer your first question, you can certainly pass parameters when instantiating a new view. The relevant part of the documentation reads as follows:

When creating a new View, the options you pass — after being merged into any default options already present on the view — are attached to the view as this.options for future reference.

So your template parameter would be available in your view instance like so:

var template = this.options.template;

To answer your general question, I think what you mean is should I define a single collection containing a separate instance of the same model type to represent each topic? In which case, based on your description of your data structure, I would suggest that this is a good way to go about it. The Topic model can certainly contain a property to identify its template.

Upvotes: 1

Related Questions