在dojo/aspect中捕获异步调用响应

t1qtbnec  于 2022-12-16  发布在  Dojo
关注(0)|答案(1)|浏览(169)

尝试在dojo/aspect before()事件中捕获异步请求的响应,然后将其传递给原始方法,如下所示:

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

wwodge7n1#

aspect.around就是你要找的。它会给予你一个可以随意调用的原始函数的句柄(因此,在你准备好的任何时候都可以异步调用--或者根本不调用)。

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

相关问题