2013-03-24 00:43:47 +00:00
|
|
|
jugglingdb-hooks(3) - Hooks and object lifecycle.
|
2013-03-26 19:34:20 +00:00
|
|
|
===================
|
|
|
|
|
|
|
|
## DESCRIPTION
|
|
|
|
|
|
|
|
Hook is a class method called on object when some event happens. List of events:
|
|
|
|
|
2013-03-27 00:47:34 +00:00
|
|
|
* `initialize`:
|
|
|
|
Called after `new Model` called.
|
|
|
|
|
|
|
|
* `create`:
|
|
|
|
Called before and after create.
|
|
|
|
|
|
|
|
* `update`:
|
|
|
|
Called before and after save (except create).
|
|
|
|
|
|
|
|
* `save`:
|
|
|
|
Called before and after save (including both create and update).
|
|
|
|
|
|
|
|
* `validate`:
|
|
|
|
Called before and after validations.
|
|
|
|
|
|
|
|
* `destroy`:
|
|
|
|
Called before and after destroy on instance.
|
|
|
|
|
2013-03-26 19:34:20 +00:00
|
|
|
|
|
|
|
Each hook except `initialize` accepts callback as first argument. This callback
|
|
|
|
should be called when hook done. All hooks called on object instance, but it's
|
|
|
|
not recommended to use `this` for updating in all hooks where data argument
|
2013-03-27 00:47:34 +00:00
|
|
|
available (second argument for all data-related before-hooks: save, update,
|
2013-03-26 19:34:20 +00:00
|
|
|
create).
|
|
|
|
|
|
|
|
## INITIALIZE
|
|
|
|
|
|
|
|
Initialize hook called when new object created after all setters and default
|
|
|
|
being applied.
|
|
|
|
|
|
|
|
Model.afterInitialize = function() {
|
|
|
|
this.property = 'some value;
|
|
|
|
console.log('afterInitialize called');
|
|
|
|
};
|
|
|
|
new Model; // afterInitialize called
|
|
|
|
|
|
|
|
## CREATE
|
|
|
|
|
|
|
|
Create hooks called when object created.
|
2013-03-27 00:47:34 +00:00
|
|
|
The `beforeCreate` hook accepts `data` as a second argument.
|
2013-03-26 19:34:20 +00:00
|
|
|
|
|
|
|
Model.beforeCreate = function(next, data) {
|
|
|
|
// use data argument to update object
|
|
|
|
data.createdAt = new Date();
|
|
|
|
console.log('before');
|
|
|
|
next();
|
|
|
|
};
|
|
|
|
|
|
|
|
Model.afterCreate = function(next) {
|
|
|
|
this.notifySocialNetworks();
|
|
|
|
this.sendEmailNotifications();
|
|
|
|
console.log('after');
|
|
|
|
next();
|
|
|
|
};
|
|
|
|
|
|
|
|
Model.create({foo: 'bar'}, function(err, model) {
|
|
|
|
console.log('callback');
|
|
|
|
});
|
|
|
|
|
|
|
|
Example output will be:
|
|
|
|
|
|
|
|
before
|
|
|
|
after
|
|
|
|
callback
|
|
|
|
|
|
|
|
## UPDATE
|
|
|
|
|
|
|
|
Update hooks called on each save except create.
|
2013-03-27 00:47:34 +00:00
|
|
|
The `beforeUpdate` hook accepts data as second argument.
|
2013-03-26 19:34:20 +00:00
|
|
|
Data argument only containing actual data for update, not full object data.
|
|
|
|
|
|
|
|
Model.beforeUpdate = function(next, data) {
|
|
|
|
// use data argument to update object
|
|
|
|
// in update hook data argumen only contains data for update (not
|
|
|
|
// full object)
|
|
|
|
data.updatedAt = new Date();
|
|
|
|
console.log('before');
|
|
|
|
next();
|
|
|
|
};
|
|
|
|
|
|
|
|
Model.afterUpdate = function(next) {
|
|
|
|
this.scheduleFulltextIndexUpdate();
|
|
|
|
console.log('after');
|
|
|
|
next();
|
|
|
|
};
|
|
|
|
|
|
|
|
model.updateAttributes({foo: 'bar'}, function(err, model) {
|
|
|
|
console.log('callback');
|
|
|
|
});
|
|
|
|
|
|
|
|
Example output will be:
|
|
|
|
|
|
|
|
before
|
|
|
|
after
|
|
|
|
callback
|
|
|
|
|
|
|
|
## SAVE
|
|
|
|
|
|
|
|
Save hooks called on each save, both update and create.
|
2013-03-27 00:47:34 +00:00
|
|
|
The `beforeSave` hook accepts `data` as a second argument.
|
|
|
|
For `beforeSave` hook `data` argument is the same as `this`.
|
2013-03-26 19:34:20 +00:00
|
|
|
|
|
|
|
Model.beforeSave = function(next, data) {
|
2013-03-27 00:47:34 +00:00
|
|
|
if ('string' !== typeof data.tags) {
|
|
|
|
data.tags = JSON.stringify(data.tags);
|
|
|
|
}
|
2013-03-26 19:34:20 +00:00
|
|
|
next();
|
|
|
|
};
|
|
|
|
|
|
|
|
Model.afterSave = function(next) {
|
|
|
|
next();
|
|
|
|
};
|
|
|
|
|
|
|
|
## DESTROY
|
|
|
|
|
|
|
|
Destroy hooks called when `model.destroy()` called. Please note that
|
|
|
|
`destroyAll` method doesn't call destroy hooks.
|
|
|
|
|
|
|
|
## VALIDATE
|
|
|
|
|
|
|
|
Validate hooks callen before and after validation and should be used for data
|
|
|
|
modification and not for validation. Use custom validation described in
|
|
|
|
jugglingdb-validations(3) man section.
|
|
|
|
|
|
|
|
## SEE ALSO
|
|
|
|
|
|
|
|
jugglingdb-model(3)
|
|
|
|
jugglingdb-validations(3)
|