Created
December 16, 2013 23:32
-
-
Save ritch/7996908 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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