JSFiddle - React, Tailwind, and code Playground
by Rob Cameron
JavaScript
Person = Backbone.Model.extend({
defaults:{
name:'Baby',
age:0,
children:[]
},
validate:function(attributes) {
if (attributes.age < 0) {
return "You cannot be negative years old";
}
},
initialize:function() {
console.info('welcome');
this.bind('change', function() {
console.info(this.get('name') + ' changed his name!');
});
this.bind('error', function(model, error) {
console.error(error);
console.info(this);
});
},
adopt:function(childName) {
var childrenArray = this.get('children');
childrenArray.push(childName);
this.set({children:childrenArray});
}
});
var rob = new Person({name:'Rob', age:34, children:['Jack']});
var dave = new Person({name:'Dave',age:32});
// adopt a kid
rob.adopt('Jill');
// change some attributes
rob.set({name:'Robert',age:33});
rob.set({age:-1});
// what's the record look like?
console.info(rob.attributes);