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.
    }
});
 
Peter Seliger
  • 11,747
  • 3
  • 28
  • 37
Le_Master
  • 147
  • 1
  • 2
  • 20
  • 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`. – Peter Seliger Sep 14 '22 at 14:23

1 Answers1

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);
                    })
                }
            ); 
        }
    }
});
 
Ivo Jonker
  • 306
  • 3
  • 9