e.g. I have a model in Backbone.js Content and collection of this model Contents.
the model has attribute
{ id:1 , name:'rahul ',age: 27 }
now if i want to update the model with id=1 in my collection then what will be the code ? e.g i want to update name from rahul to mehta .
Content = Backbone.Model.extend({
initialize: function() {
}
});
Contents = Backbone.Collection.extend({
model : Content,
initialize: function(models, args) {
console.log('in contents'+this.length);
}
});
what will be the code for this .?
How i will get the model from my collection of models ?
{id:1,name:mehta ,age : 27 }
and then i need to update the content of that ?should i 开发者_开发知识库need to change the complete data to the model or part of data only i need to update ?
For your first question:
var rahul = contents.get(1);
rahul.set({name: "mehta"});
Second question: this within initialize would be the collection.
Last question: you update what you want to update on the model using the set function of the model.
Please read the documentation: http://documentcloud.github.com/backbone/
精彩评论