是否可以将GET响应数据传回同一工厂?

时间:2014-08-18 22:31:50

标签: javascript angularjs rest get factory

问题:

如何从任何控制器调用getPages函数,将数据返回给控制器并用Page.details.ref响应数据替换空GET对象?< / strong>

是否有可能在工厂内发生这一切,无论哪个控制器调用该功能?

app.factory('Pages', function($http, ENV){
    var Pages = {};

    Pages.details = 
    {
        pages: 
            {
                length: 0,
                offsets: []
            },
        ref:  
            {
            //data goes here on success
            },
        getPages: function($scope) {
            return $http.get(ENV.apiEndpoint + '/' + $scope.storeSlug + '/pages.json?code=' + $scope.promoCode)
            .success(function(data){
              // I want this Pages.details.ref to be replaced on success of getPages
              Pages.details.ref = data;
              $scope.handlePagesSuccess(data);

              return data;
            })
            .error(function(data, status){
              // console.log('error:' + status);
            });
        }
    }

    return Pages;
});

控制器: 此控制器调用init请求

app.controller('RandomCtrl', function($scope, Pages){
   var handleSuccess = function (data) {
      $scope.data = data;
   }
   Pages.details.getPages($scope).success(handleSuccess);
})

控制器#2: 这个控制器只消耗一个临时版本的请求,RandomCtrl之间没有任何关系。例如,这个控制器通常是一个指令级控制器,其中父级ctrl之间没有冒泡

app.controller('OtherCtrl', function($scope, Pages){
    $scope.tempPage = Pages.details.ref;
})

调用getPages的位置无关紧要。我希望每次调用getPages时都要替换ref。

1 个答案:

答案 0 :(得分:1)

您似乎正在尝试管理工厂内的状态,这可能不是一个好主意。在工厂中传递$scope并不是一个好主意。它们应限于自己的控制器。您可以改为缓存前一次调用的承诺,并根据标志返回缓存的承诺或进行实际的服务调用。

app.factory('Pages', function($http, ENV, $q){
        var Pages = {};
        var cachedPromise = {};
        Pages.details = 
        {
            pages: 
                {
                    length: 0,
                    offsets: []
                },
            getPages: function(request) {
               //Get a request key to make sure you are returning right promise incase multiple product calls are made at the same time.
               var reqKey = request.storeSlug + request.promoCode;

               //if a call has already been made and there is a promise return it
               if(cachedPromise[reqKey]) return cachedPromise[reqKey];

               //Store the promise in the cache for lastCall retrieval
               return cachedPromise[reqKey] = $http.get(ENV.apiEndpoint + '/' + request.storeSlug + '/pages.json?code=' + request.promoCode)
                .then(function(result){
                    return result.data; //You can alter data and send as well
                }, function(data, status){
                    return $q.reject('some error'); //or return some data
                }).finally(function(){
                   //remove the cache from the map, once promise is resolved.
                   delete cachedPromise[reqKey];
               });

            }
        }
        return Pages;
    });

在你的第一个控制器中执行: -

app.controller('RandomCtrl', function($scope, Pages){
   //Build your request.
   Pages.details.getPages(request).then(function (data) {
       $scope.data = data;
    });
});

在你的第二个控制器中也是这样做的: -

app.controller('OtherCtrl', function($scope, Pages){
    //Pass the flag to get the cached data.
    Pages.details.getPages(request).then(function (data) {
       $scope.tempPage = data;
    });
});