Callback
CORMO supports following callbacks:
- [[#BaseModel.afterInitialize]]
- [[#BaseModel.afterFind]]
- [[#BaseModel.beforeValidate]]
- [[#BaseModel.afterValidate]]
- [[#BaseModel.beforeSave]]
- [[#BaseModel.afterSave]]
- [[#BaseModel.beforeCreate]]
- [[#BaseModel.afterCreate]]
- [[#BaseModel.beforeUpdate]]
- [[#BaseModel.afterUpdate]]
- [[#BaseModel.beforeDestroy]]
- [[#BaseModel.afterDestroy]]
You can register a callback as a method name or an anonymous function
class User extends cormo.BaseModel
@afterInitialize 'onAfterInitialie'
onAfterInitialie: ->
console.log 'initialized'
@afterCreate ->
console.log 'created'
var User = connection.model('User', {});
User.afterInitialize('onAfterInitialie');
User.prototype.onAfterInitialie = function () {
console.log('initialized');
};
User.afterCreate(function () {
console.log('created');
});
[[#BaseModel::constructor]] or [[#BaseModel.build]] triggers following callbacks:
- [[#BaseModel.afterInitialize]]
[[#BaseModel.create]] triggers following callbacks:
- [[#BaseModel.afterInitialize]]
- [[#BaseModel.beforeValidate]]
- [[#BaseModel.afterValidate]]
- [[#BaseModel.beforeSave]]
- [[#BaseModel.beforeCreate]]
- [[#BaseModel.afterCreate]]
- [[#BaseModel.afterSave]]
[[#Query::exec]] triggers following callbacks:
- [[#BaseModel.afterFind]]
- [[#BaseModel.afterInitialize]]
[[#BaseModel::save]] on an existing record triggers following callbacks:
- [[#BaseModel.beforeValidate]]
- [[#BaseModel.afterValidate]]
- [[#BaseModel.beforeSave]]
- [[#BaseModel.beforeUpdate]]
- [[#BaseModel.afterUpdate]]
- [[#BaseModel.afterSave]]
[[#BaseModel::destroy]] triggers following callbacks:
- [[#BaseModel.beforeDestroy]]
- [[#BaseModel.afterDestroy]]
Note that update or delete records using an [[#Query]] object does not trigger any callbacks.