deferred.notify未到达目的地

时间:2017-03-24 08:24:18

标签: angularjs promise angular-promise deferred

我有一个使用服务来获取某些数据的指令。该服务尽可能使用缓存。

如果数据在缓存中,则通知工作完美。第一次提取数据时,会触发通知,但通知似乎没有到达目的地。

我花了一些时间深入研究它,无法弄清楚出了什么问题。希望这是一个简单的错误。

指令。

kitchenApp.directive(
'chunkViewer',
[
    'v10systemModel',
    function(v10systemModel) {
        return {
            link: function($scope, element, attrs) {
                // attrs are all lower-case
                $scope.chunkID  = attrs.chunkid;
            },
            controller: function($scope) {
                v10systemModel.fetchDocument($scope.state.systemID, $scope.state.bookID)
                    .then(null, null, function(doc) {
                        $scope.chunk = doc.getChunkContents($scope.chunkID);
                    });
            },
            templateUrl: '/views/v10/directives/chunk-viewer.html',
        };
}]);

服务(部分)

service.fetchDocument = function(systemID, docID) {
    var deferred = $q.defer();

    if (systemID === cachedSystemID && docID === cachedDocID) {
        $timeout(function() {
            //
            // this one works
            //
            deferred.notify(cachedDoc);
        });
        return deferred.promise;
    }

    if (cachedDocID) {
        v10firedocs.stopWatchingDocument(RPGS_COLLECTION, cachedSystemID, cachedDocID);
        cachedSystemID = cachedDocID = cachedDC = null;
    }

    v10firedocs.getDocument(RPGS_COLLECTION, systemID, docID)
        .then(null, null, function(docObject) {
            cachedDoc = docObject;
            cachedSystemID = systemID;
            cachedDocID = docID;

            //
            // this doesn't appear to work
            //
            return deferred.notify(docObject);
        });

    return deferred.promise;
};

0 个答案:

没有答案