Skip to content

Commit

Permalink
Removed AOP advice for existing objects
Browse files Browse the repository at this point in the history
  • Loading branch information
kriszyp committed Jan 10, 2011
1 parent 7ecc386 commit 609c9c5
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 44 deletions.
27 changes: 2 additions & 25 deletions README.md
Expand Up @@ -283,7 +283,8 @@ the base method:

The before() advice can return an array that will be used as the arguments for the
inherited function. If nothing is returned, the original calling arguments are passed to
the inherited function.
the inherited function. If Compose.stop is returned, the inherited function will not be
called.

The around function allows one to closure around an overriden method to combine
functionality. For example, we could override the render function in Widget, but still
Expand All @@ -302,30 +303,6 @@ call the base function:
});
</pre>

You can also apply aspects to existing instances. For example, we can add a listener
for each time render is called on a widget instance:
<pre>
var widget = new Widget(node);
Compose.after(widget, "render", function(){
console.log("render called");
});
</pre>

You can also use the aspect functions as methods that are applied to their instances.
For example, we could add the after() function as an on() method:
<pre>
var after = Compose.after;
WidgetEventEmitter = Compose(Widget, {
on: after
});
var widget = new WidgetEventEmitter(node);
widget.on("render", function(){
console.log("render called");
});
</pre>

All the aspect functions (before, after, and around) can be applied to existing instances or used as methods as demonstrated above.

### Composition Control: Method Aliasing and Exclusion
One of the key capabilities of traits-style composition is control of which method to
keep or exclude from the different components that are being combined. The from()
Expand Down
21 changes: 2 additions & 19 deletions lib/compose.js
Expand Up @@ -123,24 +123,7 @@ define([], function(){
// aspect applier
function aspect(handler){
return function(target, methodName, advice){
if(!advice){
if(methodName){
advice = methodName;
methodName = target;
}else{
// single argument, creating a decorator
advice = target;
return Decorator(install);
}
target = this;
}
install.call(target, methodName);
/*return {
cancel: function(){
// TODO: Add cancel method
}
}*/
function install(key){
return Decorator(function(key){
var baseMethod = this[key];
if(baseMethod && !(baseMethod.install)){
// applying to a plain method
Expand All @@ -151,7 +134,7 @@ define([], function(){
return handler(this, this[key], advice);
});
}
}
});
};
};
// around advice, useful for calling super methods too
Expand Down

0 comments on commit 609c9c5

Please sign in to comment.