Skip to content

Instantly share code, notes, and snippets.

@sagar-ganatra
Created October 5, 2012 14:05
Show Gist options
  • Save sagar-ganatra/3839963 to your computer and use it in GitHub Desktop.
Save sagar-ganatra/3839963 to your computer and use it in GitHub Desktop.
A Backbone Model without constructor
//Create a Car Model
var Car = Backbone.Model.extend({
//Validate the Car Model
validate: function(attr) {
if( !(attr.brand && attr.model && attr.color) ) {
return "Error Occurred";
}
},
//Initialize is called after the Model object is constructed
initialize: function() {
//if there's an validation error invoke this function
this.bind("error", function (model, error) {
console.log(error);
});
console.log('Inside initialize');
}
});
//Create an instance of Car
var carObj = new Car({brand: 'Ford', model: 'Figo'});
//carObj is created successfully
console.log(carObj);
//check if it is valid
console.log('Is Model valid: ' + carObj.isValid());
//This will throw an error
carObj.set({name: 'Honda'});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment