6

我正在尝试使用backbone.js model.set 属性。在执行 todo1.set 后,第二个警报功能应该触发。然而事实并非如此。
这是 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'));
4

1 回答 1

13

您应该传递一个对象而不是单独传递参数。

todo2.set({"title":'Title set'});
于 2013-06-08T20:20:10.603 回答