我正在尝试使用backbone.js model.set属性.第二个警报功能应在实现todo1.set后触发.但事实并非如此.
这是jsfiddle链接: http://jsfiddle.net/SGEkn/
这是jsfiddle链接: http://jsfiddle.net/SGEkn/
var Todo = Backbone.Model.extend({ defaults: { title: "Harsh",completed: false },initialize: function() { console.log('This model has been initialized.') } }); var todo2 = new Todo({ title: 'Set through instantiation.',completed: true }); console.log('Todo title: ' + todo2.get('title')); console.log('Todo completed ' + todo2.get('completed')); alert("ok"); todo2.set("title",'Title set'); alert("ok"); console.log(todo2.get('title')); todo.set("completed",false); console.log('completed: ' + todo2.get('completed'));
解决方法
您应该单独传递一个对象而不是参数.
todo2.set({"title":'Title set'});