简体   繁体   中英

Backbone: How To Bind A Model To A View

So I have some objects extended from Backbone.js:

var MyModel = Backbone.Model.extend({});
var modelInstance = new MyModel();
var MyModelView = Backbone.View.extend({});

And I'm trying to figure out how to bind my Model to its corresponding View. How does one handle data-binding in Backbone?

You pass your model instance into the view when creating it.

var modelView = new MyModelView({model: modelInstance})

From the docs :

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. There are several special options that, if passed, will be 
attached directly to the view: model, collection, el, id, className, tagName and attributes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM