您好我是新來的backbonejs的一個bug,這是設置backbonejs功能
我也遇到過,而我是想的http://addyosmani.github.com/backbone-fundamentals/#validation
一個例子,我已經創建了模型的2物體,myTodo和問題myTodo1,
如果我叫設置這樣的功能,它返回完成:虛假由於驗證錯誤
var myTodo = new Todo(); myTodo.set('completed', true, {validate: true}); console.log("completed: "+myTodo.get('completed')); /* The above code returns following log: This model has been initialized. Remember to set a title for your todo. completed: false */
爲什麼買驗證是不是下面的代碼
var myTodo1 = new Todo(); myTodo1.set('completed',true); console.log("completed: "+myTodo1.get('completed')); myTodo1.set({validate:true}) console.log("completed: "+myTodo1.get('completed')); /* The above code returns following log: This model has been initialized. underscore-test2.js:11 completed: true underscore-test2.js:28 completed: true */
雖然這兩個組的代碼做同樣的工作執行上執行的,但在第一種情況下執行驗證,但不是在第二種情況下
爲什麼?????
下面是完整的示例代碼。
var Todo = Backbone.Model.extend({ defaults: { completed: false, }, validate: function(attribs){ if(attribs.title === undefined){ return "Remember to set a title for your todo."; } }, initialize: function(){ console.log('This model has been initialized.'); this.on("invalid", function(model, error){ console.log(error); }); this.on('change:title', function(){ console.log('- Values for title have changed.'); }); } }); **var myTodo = new Todo(); myTodo.set('completed', true, {validate: true}); console.log("completed: "+myTodo.get('completed'));** **var myTodo1 = new Todo(); myTodo1.set('completed',true); console.log("completed: "+myTodo1.get('completed')); myTodo1.set({validate:true}) console.log("completed: "+myTodo1.get('completed'));**
'myTodo1.set({validate:true})'將一個名爲'validate'的屬性設置爲true。試試'myTodo1.set({validate:'hello!'}); console.log(myTodo1.get('validate'));'在你的代碼中,你會發現這與運行你的validate函數無關。 – 2013-04-06 22:37:38