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

Is it possible to pass GET response data back to same factory?

本文关键字:工厂 数据 GET 响应 是否      更新时间:2023-09-26

问题:

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

无论哪个控制器调用函数,这一切都有可能在工厂内发生吗?

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

控制器:此控制器调用初始化请求

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。

您似乎正在尝试管理工厂内部的状态,这可能不是一个好主意。在工厂里传递$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;
    });
});