【问题标题】:Capturing async call response in dojo/aspect before之前在 dojo/aspect 中捕获异步调用响应
【发布时间】:2020-08-03 23:00:10
【问题描述】:

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

【问题讨论】:

    标签: javascript dojo aop filenet-p8 ibm-content-navigator


    【解决方案1】:

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

    【讨论】:

    • 这行得通。我之前没有完全理解 aspect.around。
    猜你喜欢
    • 1970-01-01
    • 2020-08-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-09
    • 1970-01-01
    • 2015-04-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多