Skip to content
digitxp edited this page Sep 5, 2012 · 1 revision

Resourceful allows for callbacks to be inserted within its various actions.

Hooks

Hooks insert themselves in the callback chain for the method. With hooks, you can, for example, modify the resource instance or prevent the action from taking place.

  • create
  • destroy
  • filter
  • find - also fired on Resource.all()
  • get
  • save - also fired on Resource.create()
  • update
  • view

The syntax for hooks is as follows:

Creature.before('create', function(creature, callback) {
    console.log('before create');
    callback();
});

Creature.after('create', function(err, creature, callback) {
    console.log('after create');
    callback();
});

Creature.create({diet: 'carnivore', vertebrate: true}, function(err, creature) {
    console.log('i am callback');
});

Before hooks can abort the callback chain by passing an error into the callback.

Events

Events, are called after on successful completion, and unlike hooks, are not inserted into the callback chain. Resourceful uses the standard EventEmitter API for events.

  • destroy
  • error - fired when a validation fails or when a hook passes a truthy first argument
  • filter
  • find - also fired when calling Resource.all()
  • get
  • init - fired on resource definition
  • save - also fired on Resource.create()
  • update
  • view
Creature.on('error', function(err, doc) {
    console.log(err);
});

Creature.on('save', function() {
    console.log('creature saved');
});
Clone this wiki locally