bnoguchi / hooks-js Goto Github PK
View Code? Open in Web Editor NEWAugment your methods with pre and post hooks
Augment your methods with pre and post hooks
What about using hooks in base and sub classes ?
Look at this use case
var hooks = require("hooks")
var _ = require('underscore')
// Document definition
var Document = function() {}
_.extend(Document, hooks)
Document.prototype.save = function () { console.log('save') }
Document.pre('save', function(next) { console.log('pre:save'); next() })
// Book definition
var Book = function() {}
_.extend(Book, Document)
_.extend(Book.prototype, Document.prototype)
Book.post('save', function(next) { console.log('post:save'); next() })
// Page definition
function Page() {}
_.extend(Page, Document)
_.extend(Page.prototype, Document.prototype)
// test
var page = new Page
page.save()
calling page's save method will output
"pre:save"
"save"
"post:save"
the post
handler is registered within books, but still invoked in all documents see it in action
Would it be possible to include a LICENSE file with the text of the license agreement?
Our lawyers are saying that just having "License: MIT" is not sufficient.
It looks like there's a different version of hooks in npm as here on master. The one in npm has an extra ctx argument for hook which means it doesn't follow the documented api.
With 0.3.2 and 0.2.1
var hooks = require('hooks');
function Doc(data) {
this.name = data.name;
}
// Add hooks' methods: `hook`, `pre`, and `post`
for (var k in hooks) {
Doc[k] = hooks[k];
}
Doc.prototype.foo = function() {
console.log('foo');
this.name = this.name.toUpperCase();
};
Doc.prototype.getBar = function() {
console.log('bar');
return 'bar';
};
Doc.pre('foo', function(next) {
console.log('pre foo');
next();
});
Doc.pre('getBar', function(next) {
console.log('pre getBar');
next();
});
var doc = new Doc({name: 'Joe'});
doc.foo(); // good
console.log(doc.getBar()); // prints return value of undefined
console.dir(doc); // { name: 'JOE' }
Output:
pre foo
foo
pre getBar
bar
undefined
{ name: 'JOE' }
Maybe I am doing something wrong...? Everything is executed correctly, but the return value ends up being undefined
. Tried stepping through things and debugging it, not sure what happens. The return value is correct both in the ret
in _done
and in fnWrapper
in once
.
npm info fetch http://registry.npmjs.org/hooks/-/hooks-0.1.9.tgz
npm info calculating sha1 /var/folders/L5/L5b8zccxFYiv8aXPH4Xpr++++TI/-Tmp-/npm-1311158342434/1311158345082-0.03321287292055786/tmp.tgz
npm info shasum 064e8bba3fca0e6611408c011ba7958c1055bb4e
npm ERR! Error: Using '>=' with 0.4.x makes no sense. Don't do it.
npm ERR! at /usr/local/Cellar/npm/0.2.11-5/libexec/lib/utils/semver.js:94:29
npm ERR! at String.replace (native)
npm ERR! at replaceXRange (/usr/local/Cellar/npm/0.2.11-5/libexec/lib/utils/semver.js:88:25)
npm ERR! at Array.map (native)
npm ERR! at replaceXRanges (/usr/local/Cellar/npm/0.2.11-5/libexec/lib/utils/semver.js:84:17)
npm ERR! at Array.map (native)
npm ERR! at toComparators (/usr/local/Cellar/npm/0.2.11-5/libexec/lib/utils/semver.js:66:8)
npm ERR! at Object.validRange (/usr/local/Cellar/npm/0.2.11-5/libexec/lib/utils/semver.js:138:11)
npm ERR! at testEngine (/usr/local/Cellar/npm/0.2.11-5/libexec/lib/utils/read-json.js:218:44)
npm ERR! at /usr/local/Cellar/npm/0.2.11-5/libexec/lib/utils/read-json.js:159:10
npm ERR! Report this *entire* log at
npm ERR! or email it to
npm ERR! Just tweeting a tiny part of the error will not be helpful.
npm info range [email protected]
npm not ok
Previously: http://stackoverflow.com/questions/6760622/failed-installation-of-calipso-under-nodejs
Hi,
I have something like this.
Document.pre('beforeSave' , function(next) {
// do something
next();
});
Document.prototype.save = function(callback) {
//save the document async and callbacks async
callback(err, doc);
};
Document.post('save', function(next, err, doc) {
//check the err and do something
});
Here i want the post to be called only after the save is finished, which mean i want the save callback to be chained with the post. At the moment, this seems to be not possible with the hooks. Is there a way to achieve this ?
Thanks.
Forgive my sin but I wrote this in coffeescript
hooks = require 'hooks'
class Test
save: ->
console.log "I HAVE BEEN SAVED!"
Test[fn] = hooks[fn] for fn of hooks
Test.hook 'save', Test::save
Test.pre 'save', true, (next, done) ->
console.log "This is before i've been saved!"
done()
next()
test = new Test()
test.save();
Easy fix is to replace done()
with setTimeout(done, 0)
, but it'd be nice if done would just work synchronously, too. Useful for situations where a pre hook is conditionally async.
Let's say I have a JavaScript constructor as follows:
function MyModel() {
}
MyModel.create = function(...);
How can I set up pre/post hooks for MyModel.create? The code doesn't seem to support this use case as it assumes the methods are from the prototype.
Nice project !
Maybe you do not know, but what you're doing seems to be like Aspect Oriented Programming (AOP). Therefore you may be interested by similar projects like surround or Javascript-Hooker :)
Please update yarn.lock file, otherwise I cannot yarn install sucess.
Using callbacks:
toySchema.pre('save', function (next) {
if (!this.created) this.created = new Date
next()
})
Using promises:
toySchema.pre('save', async function () {
if (!this.created) this.created = new Date
})
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.