【问题标题】:Deferring a Dojo Deferred推迟 Dojo Deferred
【发布时间】:2013-02-06 14:17:08
【问题描述】:

我在从小部件中的方法获取延迟返回时遇到了一点问题。该方法本身返回一个 Deferred,因为它是一个 xhrPost。代码就是这样(使用dojo 1.8)

调用代码:

quorum = registry.byId("quorumPanel");

var deferredResponse = quorum.updateSelectionCount();

deferredResponse.then(function(data){
    console.log("Success: ", data);
}, function(err){
    console.log("Error: ", err);
});

以及小部件中的代码:

updateSelectionCount: function() {

    var self = this;

    var deferredResponse = xhr.post({
        url: "ajxclwrp.php",
        content: [arguments here],
        handleAs: "json"});

    deferredResponse.then(function(response) {

    var anotherDeferred = new Deferred();

        var _boolA = true;
        var _boolB = true;
        dojo.forEach(response.result, function(relationshipInfo){
            [do a bunch of stuff here too set _boolA and/or _boolB]
        });

        self._sethasRequiredAttr(_hasRequired);
        self._setHasRequestedAttr(_hasRequested);
        self.quorumInfo.innerHTML = quorumHtml;

        // Below is not working 
        anotherDeferred.resolve('foo');
        return anotherDeferred;

    });

}

我是否需要设置另一个 promise 并使用 promise/all.我在这一点上感到困惑/沮丧。

TIA。

【问题讨论】:

    标签: dojo widget deferred


    【解决方案1】:

    .then() 方法返回另一个延迟。你只需要在里面放一个return语句。

    updateSelectionCount: function() {
    
        var self = this;
    
        var deferredResponse = xhr.post({
            url: "ajxclwrp.php",
            content: [arguments here],
            handleAs: "json"});
    
        return deferredResponse.then(function(response) {
    
            var _boolA = true;
            var _boolB = true;
            dojo.forEach(response.result, function(relationshipInfo){
                [do a bunch of stuff here too set _boolA and/or _boolB]
            });
    
            self._sethasRequiredAttr(_hasRequired);
            self._setHasRequestedAttr(_hasRequested);
            self.quorumInfo.innerHTML = quorumHtml;
    
            return "foo";
        });
    
    }
    

    【讨论】:

      猜你喜欢
      • 2016-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多