0

Trying to capture response of a async request in dojo/aspect before() event before handing it off to the original method as below:

aspect.before(ecm.model.SearchTemplate.prototype, "_searchCompleted", function(response, callback, teamspace){
    var args = [];
    if(response.num_results==0 && isValidQuery){
        var args = [];
        var requestParams = {};
        requestParams.repositoryId = this.repository.id;
        requestParams.query = query;
        
        Request.invokePluginService("samplePlugin", "sampleService",
            {
                requestParams: requestParams,
                requestCompleteCallback: lang.hitch(this, function(resp) {  // success
                    //call stack doesnt enter this code block before returning params to the original 
                    //function
                    resp.repository = this.repository;
                    args.push(resp);
                    args.push(callback);
                    args.push(teamspace);
                })
            }
        );
        return args; //args is empty as the response is not captured here yet.
    }
});
 
1
  • Regarding the former AOP tag, wrapping and reassigning already declared functionality (be it functions or methods) misses any aspect of AOP. Any language which wants to qualify for the latter has to provide abstraction levels for at least Joinpoint, Advice and Aspect. The use case described by the OP should be referred to as method modification, and JavaScript of cause is well suited for this scenario and could easily provide a complete target/context aware toolset of method modifiers like around, before, after, afterThrowing and afterFinally via Function.prototype. Commented Sep 14, 2022 at 14:23

1 Answer 1

1

aspect.around is what you're looking for. It will give you a handle to the original function you can call at will (thus, async at any time you're ready - or never at all).

aspect.around(ecm.model.SearchTemplate.prototype, "_searchCompleted", function advisingFunction(original_searchCompleted){
    return function(response, callback, teamspace){
        var args = [];
        if(response.num_results==0 && isValidQuery){
            var args = [];
            var requestParams = {};
            requestParams.repositoryId = this.repository.id;
            requestParams.query = query;
            
            Request.invokePluginService("samplePlugin", "sampleService",
                {
                    requestParams: requestParams,
                    requestCompleteCallback: lang.hitch(this, function(resp) {  // success
                        //call stack doesnt enter this code block before returning params to the original 
                        //function
                        resp.repository = this.repository;
                        args.push(resp);
                        args.push(callback);
                        args.push(teamspace);
                        original_searchCompleted.apply(this,args);
                    })
                }
            ); 
        }
    }
});
 
Sign up to request clarification or add additional context in comments.

1 Comment

This worked. I did not fully understand aspect.around earlier.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.