我注意到当我有如下代码时:http: //jsfiddle.net/MtULf/1/
window.Todo = Backbone.Model.extend({
defaults: {
"title": ""
},
validate: function() {
if (this.get("title") === "")
return "Title is missing!"
}
});
var todo = new Todo();
console.log("Expects blank: ", todo.get("title"));
todo.set({ title: "A valid title" });
console.log("Expects 'A valid title': ", todo.get("title"));
todo.set("title", "");
console.log("Expects blank: ", todo.get("title"));
一切set(...)
似乎都失败了:todo.get("title")
总是返回空字符串
但是,如果我删除defaults
,我会得到预期的输出:http: //jsfiddle.net/MtULf/2/
window.Todo = Backbone.Model.extend({
validate: function() {
if (this.get("title") === "")
return "Title is missing!"
}
});