Skip to content

Instantly share code, notes, and snippets.

@ritch
Created December 16, 2013 23:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ritch/7996908 to your computer and use it in GitHub Desktop.
Save ritch/7996908 to your computer and use it in GitHub Desktop.
// DAO
function create(obj, callback) {
var Model = this;
var ctx = new EventEmitter();
ctx.data = obj;
ctx.callback = callback;
Model.emit('create:start', ctx);
this._adapter.create(obj, function(err, result) {
ctx.result = result;
ctx.emit('finished');
Model.emit('create:finish', ctx);
callback(err, result);
});
}
// hook usage
MyModel.on('create:start', function(ctx) {
var start = Date.now();
console.log(ctx.data); // => {...}
ctx.once('finished', function() {
console.log('it took %s ms', Date.now() - start);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment