2012-10-30 55 views
0

我正在嘗試一些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() 
+0

看看這個問題: [http://stackoverflow.com/questions/7915352/model-change-event-order-in-backbone-js][1] [1]:http://stackoverflow.com/questions/7915352/ model-change-event-order-in-backbone-js – thedev

+4

版本0.3.3沒有'on',它使用'bind'。較新版本的Backbone使用'on'來匹配jQuery。升級到最新版本的Backbone,然後重試。 –

回答

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>