Search code examples
javascriptdojofilenet-p8ibm-content-navigatormethod-modifier

Capturing async call response in dojo/aspect before


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.
    }
});
 

Solution

  • 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);
                        })
                    }
                ); 
            }
        }
    });