Захват асинхронного c ответа на вызов в додзё / аспекте перед - PullRequest
0 голосов
/ 04 августа 2020

Попытка получить ответ на асинхронный c запрос в событии 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.
    }
});
 

1 Ответ

1 голос
/ 04 августа 2020

aspect.around - это то, что вы ищете. Он даст вам дескриптор исходной функции, которую вы можете вызвать по своему желанию (таким образом, asyn c в любое время, когда вы будете готовы - или никогда вообще).

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