Skip to content

Instantly share code, notes, and snippets.

@subtleGradient
Created May 6, 2009 01:28
Show Gist options
  • Select an option

  • Save subtleGradient/107316 to your computer and use it in GitHub Desktop.

Select an option

Save subtleGradient/107316 to your computer and use it in GitHub Desktop.
// -*- Mode: MooTools Javascript; tab-width: 4; -*-
// @require 'mootools'
var MyClass = new Class({
Implements: Events,
initialize: function(){
},
method: function(arg){
console.log('MyClass#method');
// console.log(this);
// console.log(arg);
// console.log(!!arguments.callee._old_);
// console.log(Array.prototype.slice.call(arguments));
}
});
var myClassInstance = new MyClass();
myClassInstance.addEvents({
'before method': function(){
console.log('before method');
},
'after method': function(){
console.log('after method');
}
});
myClassInstance.method(123);
function Wrap(Klass, methodName){
var beforeEventName = 'before ' + methodName;
var afterEventName = 'after ' + methodName;
function noo(){
try{
this.fireEvent(beforeEventName);
return noo._old_.apply(this, arguments);
}catch(e){
throw e;
}finally{
this.fireEvent(afterEventName);
};
};
noo._old_ = Klass.prototype[methodName];
return Klass.prototype[methodName] = noo;
}
Wrap(MyClass, 'method');
myClassInstance.method(123);
Wrap(MyClass, 'method');
myClassInstance.method(123);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment