Created
October 5, 2012 14:05
-
-
Save sagar-ganatra/3839963 to your computer and use it in GitHub Desktop.
A Backbone Model without constructor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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