对象未在Angular Service中正确更新

时间:2015-01-16 17:40:49

标签: javascript angularjs

我认为这与JS Closures的工作方式有关,但我并不完全确定。我正在使用AngularJS服务来管理我的应用程序中使用的模型的生命周期。该服务使用fetch()save()的组合来运行GET和POST请求从API获取并更新模型。在我fetch()对象之后,我尝试将结果放入服务中的对象中,稍后可以在该对象中获取该对象。我的问题是,在成功save()之后,我将结果放在同一个对象中,基本上是"更新"客户端上的对象与服务器上的正确对象(因此POST的结果只是有效负载的回显,如果一切都成功)。

问题是我的对象没有持久化,并且对save()的所有后续调用都包含"陈旧"未完全更新的对象。

这是我的服务:

app.factory('MailboxSubscription', function (API, $q, $stateParams, $rootScope) {

    var Subscription = null; //THIS IS MODEL THAT I TRY TO UPDATE CONSTANTLY
    var isBusy = false;
    var service = {};
    var deferred;

    var defaultFailure = function(res){

    }

    service.fetch = function (success, force, failure) {

        if(!failure){ failure = defaultFailure;}


        if(isBusy){
            deferred.promise.then(success, failure);
            return deferred.promise;
        }else{
            deferred = $q.defer();
        }

        if(Subscription && !force){ // ONCE THE MODEL HAS BEEN FETCHED ONCE, IT STAYS IN MEMORY AND ALL SUBSEQUENT CALLS WILL SKIP THE API CALL AND JUST RETURN THIS OBJECT
            deferred.resolve(Subscription);
        }else{
            //Make the API call to get the data

            //Make the API call to get the data
            if(typeof(success) === 'function'){
                var ServiceId = $stateParams.serviceId;
            }else{
                var ServiceId = success;
            }

            isBusy = true;
            API.Backups.O365.Exchange.get({id : ServiceId || $stateParams.serviceId}, function(res){
                isBusy = false;
                if(res.success){
                    Subscription = res.result;  // ON A FIRST-TIME FETCH, THIS API CALL IS USED TO GET THE MODEL
                    deferred.resolve(Subscription);
                }else{
                    deferred.reject(res);
                }
            }, function(res){
                isBusy = false;
                deferred.reject(res);
            });
        }

        deferred.promise.then(success, failure);

        return deferred.promise;

    }

    service.save = function(success, failure){
        if(!failure){ failure = function(){};}
        if(!success){ success = function(){};}

        var deferred = $q.defer();

        API.Backups.O365.Exchange.update({id :$rootScope.ServiceId || $stateParams.serviceId}, Subscription, function(res){
            if(res.success){
                Subscription = res.result;  // AFTER AN UPDATE IS MADE AND THE OBJECT IS SAVED, I TRY TO SET THE RESULT TO Subscription.
                deferred.resolve(res);
            }else{
                deferred.reject(res);
            }
        }, function(res){
            deferred.reject(res);
        });

        deferred.promise.then(success, failure);

        return deferred.promise;
    }


    service.get = function(){
        return Subscription;
    }

    return service;


});

因此问题似乎源于尝试使用Subscription作为存储模型的集中资源,但模型未正确更新。

1 个答案:

答案 0 :(得分:0)

如果您希望在整个服务中更新该订阅模型,我建议您在控制器中调用MailboxSubscription.fetch()MailboxSubscription.save()时使用MailboxSubscription.get()。{ {1}})您的通话方式。

then(

另外,我创建了一个working jsfiddle来简化您的用例。它工作正常。也许有一些东西可以从中闪闪发光(我使用$ timeout来欺骗你的API调用)。