$q.all(promises)和promises对象的结构来收集返回的数据
$q.all(promises)and structure of promises object to collect the returned data
我正在使用 Angularjs $q.all(promises)
进行多次 REST 调用,然后在承诺成功后收集数据。我有以下关注。
如果“promises 是简单的数组那么它就可以工作Plunker
var promises = [
Users.query().$promise,
Repositories.query().$promise
];
如果 "promises" 是简单的对象那么它也可以工作 Plunker
var promises = {
users: Users.query().$promise,
repos: Repositories.query().$promise
};
如果 "promises" 是嵌套对象,则它不起作用。根据我的要求,我需要嵌套对象来记住输入参数。 Plunker
var promises = {
users: {"phx":Users.query().$promise},
repos: {"phx":Repositories.query().$promise}
};
这些plunkr只是为了模拟我的问题。但是我希望在实际项目中使用这种方法来满足以下要求。
I have list of 12 product
Each product has "details", "benefits" and "offers" data
- I have separate REST API services for "details", "benefits" and "offers" having :productID as parameter
I am making call in following order
a. Loop for each cards
b. For each card, make a REST API call for "details", "benefits" and "offers"
c. Add #b steps into "promises" object
d. call
$q.all(promises).then(function(results) {
// Here need logic to compile the result back to product
// and corresponding "details", "benefits" and "offers" mapping
}
and get the data back
以下是 json 结构,我需要收集我的回复。
{
"prod1": {
"benefits": {},
"offers": {},
"pages": {
"productPage": {}
}
}
},
"common": {
"benefits": {},
"pages": {
"commonBenefit": {}
},
"others": {}
}
我怎样才能做到这一点?
如果实在需要的话,可以这样用$q.all
包窝:
var promises = {
users: $q.all({"phx": Users.query().$promise}),
repos: $q.all({"phx": Repositories.query().$promise})
};
我正在使用 Angularjs $q.all(promises)
进行多次 REST 调用,然后在承诺成功后收集数据。我有以下关注。
如果“promises 是简单的数组那么它就可以工作Plunker
var promises = [ Users.query().$promise, Repositories.query().$promise ];
如果 "promises" 是简单的对象那么它也可以工作 Plunker
var promises = { users: Users.query().$promise, repos: Repositories.query().$promise };
如果 "promises" 是嵌套对象,则它不起作用。根据我的要求,我需要嵌套对象来记住输入参数。 Plunker
var promises = { users: {"phx":Users.query().$promise}, repos: {"phx":Repositories.query().$promise} };
这些plunkr只是为了模拟我的问题。但是我希望在实际项目中使用这种方法来满足以下要求。
I have list of 12 product
Each product has "details", "benefits" and "offers" data
- I have separate REST API services for "details", "benefits" and "offers" having :productID as parameter
I am making call in following order
a. Loop for each cards
b. For each card, make a REST API call for "details", "benefits" and "offers"
c. Add #b steps into "promises" object
d. call
$q.all(promises).then(function(results) { // Here need logic to compile the result back to product // and corresponding "details", "benefits" and "offers" mapping }
and get the data back
以下是 json 结构,我需要收集我的回复。
{
"prod1": {
"benefits": {},
"offers": {},
"pages": {
"productPage": {}
}
}
},
"common": {
"benefits": {},
"pages": {
"commonBenefit": {}
},
"others": {}
}
我怎样才能做到这一点?
如果实在需要的话,可以这样用$q.all
包窝:
var promises = {
users: $q.all({"phx": Users.query().$promise}),
repos: $q.all({"phx": Repositories.query().$promise})
};