问题描述
我正在使用Angularjs $q.all(promises)
进行多个REST调用,然后在诺言成功后收集数据.我有以下关注者.
-
如果承诺是简单数组,那么它会起作用柱塞
var promises = [ Users.query().$promise, Repositories.query().$promise ];
-
如果"promises"是简单的对象,那么它也可以使用柱塞
var promises = { users: Users.query().$promise, repos: Repositories.query().$promise };
-
如果"promises"是嵌套对象,则它不起作用.根据我的需求,我需要嵌套对象来记住输入参数. 柱塞
var promises = { users: {"phx":Users.query().$promise}, repos: {"phx":Repositories.query().$promise} };
这些plunkr只是用来模拟我的问题.但是我希望这种方法在实际项目中满足以下要求.
以下是我需要收集响应的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})
};
I am using Angularjs $q.all(promises)
to make multiple REST call and then collecting the data once promise is successful. I have following following.
If "promises is simple array then it works Plunker
var promises = [ Users.query().$promise, Repositories.query().$promise ];
If "promises" is simple object then also it works Plunker
var promises = { users: Users.query().$promise, repos: Repositories.query().$promise };
If "promises" is nested object then it is not working. For my requirement I need nested object to remember the input parameters. Plunker
var promises = { users: {"phx":Users.query().$promise}, repos: {"phx":Repositories.query().$promise} };
These plunkr are just to simulate my problem. However I want this approach in real project for following requirement.
Following is json structure I needed to collect my response.
{
"prod1": {
"benefits": {},
"offers": {},
"pages": {
"productPage": {}
}
}
},
"common": {
"benefits": {},
"pages": {
"commonBenefit": {}
},
"others": {}
}
How can I achieve this?
If you really need it, you can wrap the nest with $q.all
like this:
var promises = {
users: $q.all({"phx": Users.query().$promise}),
repos: $q.all({"phx": Repositories.query().$promise})
};
这篇关于$ q.all(promises)和promise对象的结构来收集返回的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!