我在第一次看到backbone.js。我目前正在查看模型驗證,但是我直接從教學文本中獲取的這個測試腳本沒有按預期觸發錯誤事件。Backbone.js新手 - 無法獲取模型錯誤事件觸發
Person = Backbone.Model.extend({
// If you return a string from the validate function,
// Backbone will throw an error
validate: function(attributes) {
if (attributes.age < 0 && attributes.name != "Dr Manhatten") {
return "You can't be negative years old";
}
},
initialize: function() {
alert("Welcome to this world");
this.bind("error", function(model, error) {
// We have received an error, log it, alert it or forget it :)
alert(error);
});
}
});
var person = new Person;
person.set({ name: "Mary Poppins", age: -1 });
// Will trigger an alert outputting the error
var person = new Person;
person.set({ name: "Dr Manhatten", age: -1 });
// God have mercy on our souls
我來測試這個頁面是死的簡單如下:
<html>
<body>
<script type="text/javascript" src="Scripts/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="Scripts/underscore-min.js"></script>
<script type="text/javascript" src="Scripts/backbone-min.js"></script>
<script type="text/javascript" src="Scripts/test4.js"></script>
</body>
</html>
所有我看到的是兩個「歡迎來到這個世界」的警報。有任何想法嗎?
謝謝,看起來像我支付了$ 3一嚴重脫離過期教程!非常感激。 – getsetcode 2013-03-19 14:14:56
@tomtroughton公平地說,0.9.10只有2個月的時間:) – nikoshr 2013-03-19 14:16:26