Model
No, your models do not initialize other MVVM objects.
Make sure that they are only responsible for determining the data that they will wear and how they will be stored.
var CoolModel = Backbone.Model.extend({ defaults: function() { return { coolness: 'extreme', color: 'red' }; } }; var myModel = new CoolModel;
View
Your views should contain an initialization function that will be called automatically using the Backbone.View "parent":
var CoolView = Backbone.View.extend({ doSomething: function() { ... }, doSomethingElse: function() { ... }, initialize: function() { this.listenTo(this.model, 'eventA', this.doSomething); this.listenTo(this.model, 'eventB', this.doSomethingElse); } });
APPVIEW
When you actually create a view object, you go through the model to which it is attached. And this can happen anywhere in your code (but usually at the application level):
renderSomething: function(todo) { var view = new CoolView({model: myModel});
That is, your application combines a model and a view.
source share