$ q.all(promises)和promise对象的结构来收集返回的数据 [英] $q.all(promises)and structure of promises object to collect the returned data

查看:122
本文介绍了$ q.all(promises)和promise对象的结构来收集返回的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Angularjs $q.all(promises)进行多个REST调用,然后在诺言成功后收集数据.我有以下关注者.

  1. 如果承诺是简单数组,那么它会起作用柱塞

    var promises = [
       Users.query().$promise,
       Repositories.query().$promise
    ];
    

  2. 如果"promises"是简单的对象,那么它也可以使用柱塞

    var promises = {
       users: Users.query().$promise,
       repos: Repositories.query().$promise
    };
    

  3. 如果"promises"是嵌套对象,则它不起作用.根据我的需求,我需要嵌套对象来记住输入参数. 柱塞

    var promises = {
       users: {"phx":Users.query().$promise},
       repos: {"phx":Repositories.query().$promise}
    };
    

这些plunkr只是用来模拟我的问题.但是我希望这种方法在实际项目中满足以下要求.

  1. 我有12个产品的列表

  2. 每种产品都有详细信息",好处"和提供"数据

  3. 我使用:productID"作为参数的细节",好处"和优惠"具有单独的REST API服务
  4. 我正在按以下顺序拨打电话

    a.循环播放每张卡

    b.对于每张卡,请对详细信息",好处"和要约"进行REST API调用

    c.将#b步骤添加到承诺"对象中

    d.呼叫

    $q.all(promises).then(function(results) {
        // Here need logic to compile the result back to product 
        // and corresponding  "details", "benefits" and "offers" mapping
      } 
    

    并取回数据

以下是我需要收集响应的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})
};

plnkr.co

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.

  1. If "promises is simple array then it works Plunker

    var promises = [
       Users.query().$promise,
       Repositories.query().$promise
    ];
    

  2. If "promises" is simple object then also it works Plunker

    var promises = {
       users: Users.query().$promise,
       repos: Repositories.query().$promise
    };
    

  3. 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.

  1. I have list of 12 product

  2. Each product has "details", "benefits" and "offers" data

  3. I have separate REST API services for "details", "benefits" and "offers" having :productID as parameter
  4. 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

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

plnkr.co

这篇关于$ q.all(promises)和promise对象的结构来收集返回的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆