将数据从简单函数返回到 UI-router 的问题已解决
Problems with returning data from simple function to UI-router resolve
我已经尝试了我能找到的所有方法,建议将 promise 对象返回到 resolve,这样我的函数将异步处理,但我仍然无法返回正确的数据 - 请指教:
.state("app.deals.private", {
url: "/private/",
resolve: {
dealsFeatured: function(Deals, $rootScope) {
return Deals.getDeals('FEATURED', function(data) {
// console.log('deals featured', data); // DATA IS HERE
$rootScope.loading = false;
return data;
});
},
dealsGeneral: function(Deals, $rootScope, dealsFeatured) {
return Deals.getDeals('GENERAL', function(data) {
// console.log('deals general', data); // DATA IS HERE
$rootScope.loading = false;
return data;
});
},
dealsCats: function(Deals, dealsFeatured, dealsGeneral, $q) {
// ALL DEPENDENCIES HAVE DATA HERE
// build a promise object
var deferred = $q.defer();
Deals.mergeCats(dealsFeatured, dealsGeneral, $q, function(data){
console.log("received dealsCats: " + data);
//BUT....NOTHING IS RETURNED HERE, WHY NOT?!?!?!?
deferred.resolve(data);
});
//return the promise object
return deferred.promise;
},
这是我服务中的代码(请注意,数据是正确的,并在我回调之前输出到控制台:
api.service('Deals', function(){
this.mergeCats = function(dealsFeatured, dealsGeneral, $q, callback){
// make one big array
var allCats = [];
if(Array.isArray(dealsFeatured.data.discountTermsWithCounters.cats)){
allCats = dealsFeatured.data.discountTermsWithCounters.cats;
}
if(Array.isArray(dealsGeneral.data.discountTermsWithCounters.cats)){
allCats = allCats.concat(dealsGeneral.data.discountTermsWithCounters.cats);
}
// now recreate the array into something we can work with
var newCats = [];
angular.forEach(allCats, function(value, key){
if(newCats[value.title]){
newCats[value.title].count += value.count;
} else {
newCats[value.title] = value;
}
});
// after all is completed, return data to the promise object
$q.all(allCats).then(function () {
console.log('newCats',newCats); // CORRECT DATA IS OUTPUTTED HERE
// newCats data looks like this in console:
// Array: [Herbs & Homeopathic: Object, Frozen: Object, Groceries: Object, Body Care: Object, General Merchandise: Object…]
callback(newCats);
});
};
});
请指教,我错过了什么?
您对 dealsCats
和 Deals.mergeCats
以及如何以及为什么使用和传入 $q
作为参数感到困惑。
mergeCats
做 $q.all
,returns 一个承诺,但是 mergeCats
只做同步操作。不需要 $q
、承诺或回调:
因此,将 Deals.mergeCats
更改为以下内容:
this.mergeCats = function(dealsFeatured, dealsGeneral){
// some other code here that creates allCats
return allCats;
}
出于同样的原因,dealsCats
不需要 $q.defer
:
resolve: {
//...,
dealsCats: function(Deals, dealsFeatured, dealsGeneral){
return Deals.mergeCats(dealsFeatured, dealsGeneral);
}
}
我已经尝试了我能找到的所有方法,建议将 promise 对象返回到 resolve,这样我的函数将异步处理,但我仍然无法返回正确的数据 - 请指教:
.state("app.deals.private", {
url: "/private/",
resolve: {
dealsFeatured: function(Deals, $rootScope) {
return Deals.getDeals('FEATURED', function(data) {
// console.log('deals featured', data); // DATA IS HERE
$rootScope.loading = false;
return data;
});
},
dealsGeneral: function(Deals, $rootScope, dealsFeatured) {
return Deals.getDeals('GENERAL', function(data) {
// console.log('deals general', data); // DATA IS HERE
$rootScope.loading = false;
return data;
});
},
dealsCats: function(Deals, dealsFeatured, dealsGeneral, $q) {
// ALL DEPENDENCIES HAVE DATA HERE
// build a promise object
var deferred = $q.defer();
Deals.mergeCats(dealsFeatured, dealsGeneral, $q, function(data){
console.log("received dealsCats: " + data);
//BUT....NOTHING IS RETURNED HERE, WHY NOT?!?!?!?
deferred.resolve(data);
});
//return the promise object
return deferred.promise;
},
这是我服务中的代码(请注意,数据是正确的,并在我回调之前输出到控制台:
api.service('Deals', function(){
this.mergeCats = function(dealsFeatured, dealsGeneral, $q, callback){
// make one big array
var allCats = [];
if(Array.isArray(dealsFeatured.data.discountTermsWithCounters.cats)){
allCats = dealsFeatured.data.discountTermsWithCounters.cats;
}
if(Array.isArray(dealsGeneral.data.discountTermsWithCounters.cats)){
allCats = allCats.concat(dealsGeneral.data.discountTermsWithCounters.cats);
}
// now recreate the array into something we can work with
var newCats = [];
angular.forEach(allCats, function(value, key){
if(newCats[value.title]){
newCats[value.title].count += value.count;
} else {
newCats[value.title] = value;
}
});
// after all is completed, return data to the promise object
$q.all(allCats).then(function () {
console.log('newCats',newCats); // CORRECT DATA IS OUTPUTTED HERE
// newCats data looks like this in console:
// Array: [Herbs & Homeopathic: Object, Frozen: Object, Groceries: Object, Body Care: Object, General Merchandise: Object…]
callback(newCats);
});
};
});
请指教,我错过了什么?
您对 dealsCats
和 Deals.mergeCats
以及如何以及为什么使用和传入 $q
作为参数感到困惑。
mergeCats
做 $q.all
,returns 一个承诺,但是 mergeCats
只做同步操作。不需要 $q
、承诺或回调:
因此,将 Deals.mergeCats
更改为以下内容:
this.mergeCats = function(dealsFeatured, dealsGeneral){
// some other code here that creates allCats
return allCats;
}
出于同样的原因,dealsCats
不需要 $q.defer
:
resolve: {
//...,
dealsCats: function(Deals, dealsFeatured, dealsGeneral){
return Deals.mergeCats(dealsFeatured, dealsGeneral);
}
}