Skip to content

Instantly share code, notes, and snippets.

@tonylukasavage
Created March 26, 2013 18:22
Show Gist options
  • Save tonylukasavage/5247827 to your computer and use it in GitHub Desktop.
Save tonylukasavage/5247827 to your computer and use it in GitHub Desktop.
Handling model validation errors in Alloy
$.index.open();
// create an instance of the model
var model = Alloy.createModel('myModel');
// listen for the "error" event
model.on('error', function(model, error) {
Ti.API.info('error handled: ' + error);
})
// attempt to set the doomed model
model.set({title:'hi there'});
// title should be undefined
Ti.API.info('title: ' + model.get('title'));
<Alloy>
<Window/>
</Alloy>
exports.definition = {
config: {
columns: {
"title": "TEXT"
},
adapter: {
type: "sql",
collection_name: "myModel"
}
},
extendModel: function(Model) {
_.extend(Model.prototype, {
validate: function(attrs, opts) {
// let's us know the validation executed
Ti.API.info('validate executed');
// always fail validation for the purposes of this test,
// normally you would only return a value if your
// specific validation failed. Successful validation
// return nothing.
return "this always fails";
}
});
return Model;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment