Looking at some backbonejs examples on codeschoo.com, I don’t see any explicit associations between views and their models. How does a view know what model it should be associated with? Is it by convention, e.g. TodoView assumes that this.model is of type Todo?
To explicitly set the model to the view, is it just a matter of passing in the model in the constructor?
If you pass a
modelproperty to a view constructor, it will set that directly as a property of the view instance, e.g.view.model. That’s the case for a select group of other properties as well. Beyond those, properties passed to view constructors are set inview.options.So yes, you can do this:
Or change a an existing view instance’s model any time by assigning to
model:Some people set references to views in models (e.g.
model.view), but so far I’ve avoided that.http://backbonejs.org/#View-constructor