Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从不同角度构造视图模型_Javascript_Angularjs_Q_Angular Ui Router_Angular Promise - Fatal编程技术网

Javascript 从不同角度构造视图模型

Javascript 从不同角度构造视图模型,javascript,angularjs,q,angular-ui-router,angular-promise,Javascript,Angularjs,Q,Angular Ui Router,Angular Promise,在我的Angular项目中,我承诺将返回一系列产品: {"Products":[{"Code":123},{"Code":456}]} 然后,对于每个产品代码,我必须调用另外两个承诺,分别返回价格和数量 我使用ui路由器,我的解决方案中的当前实现代码如下: $stateProvider.state('root.products', { abstract: true, url: '/products',

在我的Angular项目中,我承诺将返回一系列产品:

{"Products":[{"Code":123},{"Code":456}]}
然后,对于每个产品代码,我必须调用另外两个承诺,分别返回价格和数量

我使用ui路由器,我的解决方案中的当前实现代码如下:

$stateProvider.state('root.products', {
                    abstract: true,
                    url: '/products',
                    template: '<div data-ui-view=""></div>',
                    resolve: {
                        products: ['ProductsService', function (ProductsService) {
                            return ProductsService.getProducts()
                                .then(function (response){
                                    var data = response.data;
                                    return data.Products.map(function (product) {

                                        var viewModelProduct = {};

                                        angular.copy(product, viewModelProduct);

                                        //get Price for Each Product
                                        ProductsService.getPrice(product.Code)
                                            .then(function (response) {
                                                viewModelProduct.Price = response.data.Info.Price;
                                            })

                                        //get Quantity for Each Product
                                        ProductsService.getQuantity(product.Code)
                                           .then(function (response) {
                                               viewModelProduct.Quantity = response.data.Info.Quantity;
                                           })

                                        return viewModelProduct;
                                    });
                                });
                                }]
                        }
                        })
$stateProvider.state('root.products'{
摘要:没错,
url:“/products”,
模板:“”,
决心:{
产品:['ProductsService',功能(ProductsService){
return ProductsService.getProducts()
.然后(功能(响应){
var数据=响应数据;
返回数据.Products.map(函数(产品){
var viewModelProduct={};
复制(产品、viewModelProduct);
//获取每种产品的价格
ProductsService.getPrice(product.Code)
.然后(功能(响应){
viewModelProduct.Price=response.data.Info.Price;
})
//获取每个产品的数量
ProductsService.getQuantity(产品代码)
.然后(功能(响应){
viewModelProduct.Quantity=response.data.Info.Quantity;
})
返回viewModelProduct;
});
});
}]
}
})
它是有效的,但我的问题是我是否能写得更好。我读过关于
$q
$q.all
的使用,但我真的不知道如何使用它们。 有没有什么方法可以让我更好更安全地编写上述代码

提前感谢您的帮助

它是有效的,但我的问题是我是否能写得更好

对。你可以写承诺链。优点是:错误会传播,您可以在链的末端捕获它,而不是Javascript中类似piramide的异步调用

参见下面的流程示例:

[编辑]

如果订单不重要,您可以使用
$q.all
获取承诺列表,并在所有承诺成功解决时通知您

应该是这样的:

       $q.all([
            ProductsService.getPrice(product.Code),
            ProductsService.getQuantity(product.Code)
           ])
            .then(function(result){

            viewModelProduct.Price   = result[0].data.Info.Price;
            iewModelProduct.Quantity = result[1].data.Info.Quantity; 

          }, function(error) {
              alert(error.message);
          });
甚至:

     var promises = [];
     promises.push(ProductsService.getPrice(product.Code));
     promises.push(ProductsService.getQuantity(product.Code));

       $q.all(promises).then(function(result){

            viewModelProduct.Price   = result[0].data.Info.Price;
            iewModelProduct.Quantity = result[1].data.Info.Quantity; 

          }, function(error) {
              alert(error.message);
          });

如果你需要一个接一个地履行承诺,你将使用像Maxim提到的链条。 多个主题:

  • $q
如果您需要执行更多承诺,然后解决它,您将使用“$q.all”

见文件:

  • $q
更新

不确定我是否理解得很好,但你应该使用q.all和链接的组合。 再说一次,如果我不能很好地理解你,我很抱歉

var p1 = ProductsService.getPrice(product.Code)
 .then(function (response) {
    return response.data.Info.Price;
 });

var p2 = ProductsService.getQuantity(product.Code)
         .then(function (response) {
         return response.data.Info.Quantity;
 });


    return $q.all([p1,p2]); // next "then" will get array with price and quantity respectively. 

这个问题似乎离题了,因为它正在请求代码复查。一个mod可以迁移到Codereview.SE吗?我在这里读过关于承诺链接的内容,但是我发现所有的例子通常只有一个对象返回,我不知道如何链接每个产品的两个承诺谢谢你的回复,你能让我知道我如何将我从第一次承诺中得到的每种产品的价格和数量的两个额外承诺联系起来吗?@RickyStam你想一个接一个地打电话给他们,还是想确保无论订单如何,他们都能完成?我想确保无论订单如何,他们都能完成order@RickyStam所以使用
$q.all
,参见上面的示例非常感谢您的帮助。非常感谢!