Opened 8 years ago

Last modified 2 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 8 years ago by bill

Component: GeneralEvents
Owner: set to Kris Zyp

comment:2 Changed 8 years ago by Marcel Lucas

I fixed a bug in my previously posted utility where the original method seemed to be called in wrong context

u.around = function(target, methodName, advise, context){
    //summary: dojo.aspect with context option
    context = context || null;
	//create an aspect.around in desired context
	return aspect.around(target, methodName, dojo.hitch(context,function(original){
		//return a function wrapper in the desired context or the target's contexts 
		return dojo.hitch(context||target, function(){
			//store the original arguments as array
			var args = Array.prototype.slice.call(arguments);
			//recreate ability to call the original method in its former context
			var org = dojo.hitch(target,function(){original.apply(this,args)});
			//create an object for the advise to not lose any information about the original call
			var obj = {target:target,methodName:methodName,arguments:args}
                        //call the advise in desired context
			advise.apply(this,[org,obj]);
		})
	}));
};
Last edited 8 years ago by Marcel Lucas (previous) (diff)

comment:3 Changed 3 years ago by dylan

Milestone: tbd1.12

Will consider for 1.12.

comment:4 Changed 2 years ago by dylan

Milestone: 1.131.15

Ticket planning... move current 1.13 tickets out to 1.15 to make it easier to move tickets into the 1.13 milestone.

Note: See TracTickets for help on using tickets.