是否有可能通过GET响应数据返回到同一个工厂? [英] Is it possible to pass GET response data back to same factory?
问题描述
从任何控制器,我怎么能叫 GETPAGES
函数,返回的数据返回到控制器,更换空页。 details.ref
与 GET
响应数据对象?
From any controller, how can I call the getPages
function, return the data back to the controller and replace the empty Page.details.ref
object with the GET
response data?
有可能是这一切的工厂内发生的无论哪个控制器调用函数?
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被调用。
it shouldnt matter where getPages is called from. I want ref to be replaced everytime getPages is called.
推荐答案
好像你正试图管理你的工厂,这可能不是一个好主意内部状态。此外,它是不是在工厂绕过 $范围
一个好主意。它们应限于它自己的控制器。你可以代替缓存制成,基于一个标志previous叫你既可以返回缓存的承诺或进行实际的服务电话。
It seems like you are trying to manage state inside your factory, which probably is not a good idea. Also it is not a good idea to pass around $scope
in factories. They should be limited to its own controller. You could instead cache the promise for the previous call made and based on a flag you could either return the cached promise or make the actual service call.
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;
});
在你的第一个控制器做到: -
In your first controller do:-
app.controller('RandomCtrl', function($scope, Pages){
//Build your request.
Pages.details.getPages(request).then(function (data) {
$scope.data = data;
});
});
在你的第二个控制器只是做相同的: -
In your second controller just do the same:-
app.controller('OtherCtrl', function($scope, Pages){
//Pass the flag to get the cached data.
Pages.details.getPages(request).then(function (data) {
$scope.tempPage = data;
});
});
这篇关于是否有可能通过GET响应数据返回到同一个工厂?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!