You can have a look at how it is handled in Archetype Components (an Open
Source Framework based on Prototype). It's called MethodBuilder, but this is
basically a Facet in AOP.
It does not just add a before and an after for any function of a component,
but it also provides some really enjoyable tools like having a this bound
to the component in all it's function, so you don't have to put
.bind(this) everywhere when using callbacks.
Get more informations at http://archetypejs.org :)
2008/5/16 kangax [EMAIL PROTECTED]:
I have done something similar in Class.addBehavior:
http://github.com/kangax/protolicious/tree/master/class.addbehavior.js
On the other hand, it's actually pretty easy to do this with plain
Function#wrap:
(function(p){
p.push = p.push.wrap(function(){
var args = $A(arguments), proceed = args.shift();
console.log('before: ' + args);
(function(){ console.log('after: ' + args) }).defer();
return proceed.apply(proceed, args);
})
})(Array.prototype);
It's arguably not as clean as your implementation, so some could
definitely find it useful : )
- kangax
On May 14, 12:13 pm, Pat Nakajima [EMAIL PROTECTED] wrote:
I've started putting together a way to specify per-object before/after
callbacks for any method. Probably not something to go in core, but
maybe something that would be interesting to core contributors.
There are some examples in a GitHub wiki (http://github.com/nakajima/
nakatype/wikis/callbacks), but basically, you specify callbacks like
so:
var someArray = [1,2,3];
Callbacks.add(someArray, {
before: {
push: function(entry) {
var msg = 'about to push ' + entry + ' on array: ' +
this.join(', ');
console.info(msg);
}
},
after: {
push: function(entry) {
var msg = 'just pushed ' + entry + ' on array: ' +
this.join(', ');
console.info(msg);
}
}
});
someArray.push(4);
In addition to adding 4 to someArray, the above code snippet will log
to the console before and after.
If anybody has any suggestions/criticisms/threats, please do share!
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups
Prototype: Core group.
To post to this group, send email to prototype-core@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/prototype-core?hl=en
-~--~~~~--~~--~--~---