永远不会调用骨干更改触发器

时间:2012-10-30 06:59:58

标签: backbone.js eventtrigger

我正在尝试一些backbone.js示例,无法让更改触发器工作。 “欢迎来到这个世界”警报按预期显示,但“更改我的名字”从未显示过。使用firefox调试代码会显示相同的结果,第二个警报上的断点永远不会被命中。

任何想法有什么不对?

这是我的代码:

main.html中

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>hello-backbonejs</title>
</head>
<body>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
    <script src="http://ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js"></script>
    <script src="http://ajax.cdnjs.com/ajax/libs/underscore.js/1.1.6/underscore-min.js"></script>
    <script src="http://ajax.cdnjs.com/ajax/libs/backbone.js/0.3.3/backbone-min.js"></script>

    <script src="model.js" type="text/javascript"></script>

</body>
</html>

model.js

Person = Backbone.Model.extend({
    defaults: {
        name: 'Fetus',
        age: 0
    },
    initialize: function(){
        alert("Welcome to this world");
        this.on("change:name", function(model){
            var name = model.get("name"); // 'Stewie Griffin'
            alert("Changed my name to " + name );
        });
    }
});

var person = new Person({ name: "Thomas", age: 67});
person.set({name: 'Stewie Griffin'}); // This triggers a change and will alert()

1 个答案:

答案 0 :(得分:0)

要获得正确版本的js库,我必须将main.html更改为此

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>hello-backbonejs</title>
</head>
<body>
    <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
    <script src="http://cdnjs.cloudflare.com/ajax/libs/json2/20110223/json2.js"></script>
    <script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.2/underscore-min.js"></script>
    <script src="http://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.9.2/backbone-min.js"></script>

    <script src="model.js" type="text/javascript"></script>

</body>
</html>