我第一次看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>
我所看到的只是两个“欢迎来到这个世界”警报。有任何想法吗?