I have created an object:
var Person = Backbone.Model.extend({
defaults: {
'name': '',
'age': 0
},
initialize: function() {
this.on('error', function(model, error){
console.log(error);
});
},
validate: function(attributes){
if (attributes.name == '') {
return "How do we call him!?";
}
},
changeName: function(name) {
this.set({'name':name});
},
getOlder: function() {
this.set({'age': this.get('age') +1});
}
});
I create an instance of Person and put in a blank on the name and I never received an error. But when I set a blank name on an already created instance, it fired the validation.
var teejay = new Person;
teejay.changeName('');
=> How do we call him!?
teejay.get('name');
=> ""
From what I am seeing from the Backbone source code, I see
this.set(attributes, {silent: true});
Is it right to assume that validation is only done whenever attributes are changed or set?