Opened 9 years ago
Last modified 4 years ago
#13992 new enhancement
dojo.aspect.around with context option
Reported by: | Marcel Lucas | Owned by: | Kris Zyp |
---|---|---|---|
Priority: | high | Milestone: | 1.15 |
Component: | Events | Version: | 1.7.0b1 |
Keywords: | Cc: | ||
Blocked By: | Blocking: |
Description
I just wondered whether there is no such 'context' parameter for dojo.aspect.around to call advises in a desired context.
Usecase:
I have a class that needs to intercept method calls - dojo around seemed perfect for this. I started out with something like this
dojo.aspect.around(target, "method", dojo.hitch(this,this.intercept));
Obviously this wasnt enough...
As it turned out - keeping the context of my class which intercepts the method call leads to a few more lines of code as expected.
Here an example of what it needs for an instance to internally call an 'aroundHandler' in the class context
dojo.aspect.around(target, "method", dojo.hitch(this,function(original){ console.log("immediately called"); return dojo.hitch(this, function(){ console.log("the interceptor function is called..."); console.log("...and directly calls the intended 'aroundHandler' in class context"); var args = Array.prototype.slice.call(arguments); this.aroundHandler.apply(this,[original,args]); }) }))
I added a utility method to my package to always be able to accomplish the above stated requirement:
util.around = function(target, methodName, advise, context){ //summary: dojo.aspect.around with context option context = context || null; return aspect.around(target, methodName, dojo.hitch(context,function(original){ return dojo.hitch(context||target, function(){ var args = Array.prototype.slice.call(arguments); advise.apply(this,[original,args]); }) })); };
Maybe the context option could be added to the toolkit - havent checked ´after´ and ´before´ yet
Change History (4)
comment:1 Changed 9 years ago by
Component: | General → Events |
---|---|
Owner: | set to Kris Zyp |
comment:4 Changed 4 years ago by
Milestone: | 1.13 → 1.15 |
---|
Ticket planning... move current 1.13 tickets out to 1.15 to make it easier to move tickets into the 1.13 milestone.
I fixed a bug in my previously posted utility where the original method seemed to be called in wrong context