Basically, I want my model to have its own validation logic while having the error being passed onto the View. Here's the code, and the link: http://jsfiddle.net/jancarlo000/aRFGm/3/
The result is 'undefined' instead of 'age cannot be 0'
Person = Backbone.Model.extend({
defaults: {
'FirstName':'JC'
},
initialize: function(){
console.log('new person');
},
validate: function(attr){
if (attr.Age == 0){
return 'age cannot be 0';
}
}
});
AppView = Backbone.View.extend({
initialize: function(){
this.model.bind('error', function(model, error){
alert(model); alert(error);
}());
}
});
var j = new Person();
var app = new AppView({model:j});
app.model.set({Age:0}); <=== purposely try to invoke validation error here..
//note: console's target must be result(fiddle.jshell.net)
//in Chrome for it to work...
UPDATE:
The problem was that I added an unnecessary invocation operator () at my function.
The problem is here:
AppView = Backbone.View.extend({
initialize: function(){
this.model.bind('error', function(model, error){ alert(model); alert(error); }());
}
});
You don't need the function that you bind to be invoked immediately, remove the parens:
AppView = Backbone.View.extend({
initialize: function(){
this.model.bind('error', function(model, error){ alert(model); alert(error); });
}
});
And run your code: http://jsfiddle.net/aRFGm/4/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.