1

我在从小部件中的方法返回延迟时遇到了一些问题。该方法本身返回一个 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。

4

1 回答 1

2

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

}
于 2013-02-06T14:31:37.893 回答