简体   繁体   English

如何正确履行诺言?

[英]how to properly handle promises?

In my Angular app, I'm using $odataresource for data retrieval and updates. 在我的Angular应用中,我使用$ odataresource进行数据检索和更新。

I have the following lines: 我有以下几行:

var measure = $odataresource("http://windows-10:8888/ChangeMeasure/");

            var myMeasure = measure.odata().filter("ID",$scope.ngData[index].ID)
                .single();

I, then, need to issue an update on myMeasure: 然后,我需要发布有关myMeasure的更新:

myMeasure.$update();

The trouble is - when I issue $update, myMeasure is still an unresolved promise. 问题是-当我发出$ update时,myMeasure仍然是一个未解决的承诺。

What is the proper syntax to call myMeasure.$update(); 调用myMeasure.$update();的正确语法是什么? when myMeasure promise is resolved? myMeasure承诺何时得到解决?

Looking at the documentation of ODataAngularResources try the following: 查看ODataAngularResources的文档, 尝试以下操作:

  var measure = $odataresource("http://windows-10:8888/ChangeMeasure/");
  var myMeasure = measure.odata().filter("ID",$scope.ngData[index].ID)
                  .query(function(data){
                    data[0].$update(); 
                    console.log("Everything went ok!")
                   },function(){
                    console.log("Oops, something wrong happened!")
                   }); 

Of course some more thought should go into sending the resource.update directly after receiving it. 当然,在收到资源后,应该再考虑直接发送资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM