Im just starting out backboneJs.
I did a simple example using VisualStudio2012. I added backbone.
in my main.js
var Person = Backbone.Model.extend({
defaults: {
name: 'Mr Steel',
age: 26,
occupation: 'FrontEnd Web Developer'
},
validate: function(attrs) {
if (attrs.age < 0) {
return 'Age must be a positive number.';
}
},
work: function() {
return this.get('name') + ' is working.';
}
});
But when I go to browser > console. And type the following in .. validate doesnt even get triggered.
var person = new Person();
person.get('age');//26
person.set('age', -25);//sets -25
And it sets the age to -25. thats weird!! please help.
You have to pass {validate:true}
param to set
method e.g.
person.set('age', -25, {validate:true});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With