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, stat开发者_开发百科us, 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.
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.
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.
Not sure of best practices but if you can break it into views then its good. Otherwise you'll be better off creating a view model with multiple collections in it and use them in the view.
精彩评论