Last active
December 24, 2015 02:39
-
-
Save gabanox/6732333 to your computer and use it in GitHub Desktop.
Simple model validation
This file contains hidden or 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
Simple model validation |
This file contains hidden or 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
<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.2/underscore-min.js"></script> | |
<script src="http://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.0.0/backbone-min.js"></script> |
This file contains hidden or 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
var MyModel = Backbone.Model.extend({ | |
defaults: { | |
}, | |
initialize: function(){ | |
this.on('invalid', function(model, error){ | |
console.log(error); | |
}); | |
}, | |
validate: function(validationObject){ | |
if(validationObject.age < 1){ | |
return 'Invalid age'; | |
} | |
} | |
}); | |
var myModel = new MyModel({ 'name': 'Gabriel', 'age':30}); | |
myModel.set('age', 0,{validate:true}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment