javascript backbone.js 模型.set 不工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17003406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 06:44:04  来源:igfitidea点击:

backbone.js model.set is not working

javascriptjquerybackbone.js

提问by user1801879

I'm trying to use backbone.js model.set property. The second alert function should fire off after todo1.set is being implemented. However it is not.
Here is the jsfiddle link: http://jsfiddle.net/SGEkn/

我正在尝试使用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'));

回答by Matt Whipple

You should be passing an object and not the arguments individually.

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

todo2.set({"title":'Title set'});