Reputation: 2902
I am learning backbone.js
, using a validate
method in it. But it is not getting triggered at set
following is my code:
JS:
var person = Backbone.Model.extend({
defaults:{
name:"Soni",
age:'40',
work:'Still searching'
},
mainWork: function(mainwork) {
return this.get('name')+" does "+mainwork+" for living";
},
validate: function(attrs) {
console.log("hi");
if (attrs.age < 1) {
return "can't end before it starts";
}
}
});
on console:
var x = new person({name:'xyz',age:37});
x.set('age',-34);
please tell me, what am I missing here.
Upvotes: 1
Views: 108
Reputation: 193301
Validate method will be called on model save
. If you want to trigger it on attribute set
you should call it will validate: true option:
x.set({age: -34}, {validate: true});
Upvotes: 3