Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a backbone view have more than one collection?

I am just starting out with backbone and am trying to set up a view which has a question list. To the left of the list I have four filters, to filter the list by language, country, status, and study. The list and each of the filters are loaded into their own collections.

My idea was to make this one view with multiple collections, but I wonder if this is best practice in backbone since all the examples I have seen only have one collection per view.

Another idea was to break in into two views with one being responsible for the filters and then a child view being responsible only for the list of questions.

Or, is it more backbone style to drop all of the collections into a model and then pass that model to my view like it mentions here: http://documentcloud.github.com/backbone/#FAQ-nested

Thanks for your ideas.

like image 726
Tom Gruner Avatar asked Apr 29 '11 13:04

Tom Gruner


People also ask

What is a backbone view?

The Backbone. js Views specify how your data looks like. They represent model's data to the users. They can be used with any JavaScript template library. They handle users input events, bind events and methods, render model and collection and interact with users.

What is collections in Backbone JS?

Collections are ordered sets of Models. We just need to extend the backbone's collection class to create our own collection. Any event that is triggered on a model in a collection will also be triggered on the collection directly.

What is backbone used for?

Backbone is known for being lightweight, as its only hard dependency is on one JavaScript library, Underscore. js, plus jQuery for use of the full library. It is designed for developing single-page web applications, and for keeping various parts of web applications (e.g. multiple clients and the server) synchronized.


2 Answers

Yes. Theoretically a view can encompass any number of inner objects/collections. It generally makes good sense to have views be as discrete as possible, but there could be reasons to wrap more than one thing in a single view.

This is all a matter of design. I don't see what creating a container model as a bucket for your collections buys you.

Don't be too concerned with the absolute best way. Sometimes it takes walking a little down the wrong path to figure out the better ways for your particular project.

like image 77
Mario Avatar answered Oct 12 '22 23:10

Mario


I think it's completely legit to pass more than one model or collection to a view - when appropriate.

Passing a model or collection to a view constructor will automatically appended that object to the view instance (so it's in this.model or this.collection) but you can also pass other data such as extra collections and they will be located in the options object (accessible from within your view as this.options.countries, etc.). Your views initialize method, if it exists, will also be passed this object.

like image 41
Mauvis Ledford Avatar answered Oct 12 '22 22:10

Mauvis Ledford