2014-01-10 27 views
1

我有调用一个WebService(callPathsToMultiTiffWS),其具有两种可能的应用:在壳体问题与异步而在WinJS

  • 完整=真
  • 完整=假

完成= false我想显示一个对话框,通知用户比webService失败和两个按钮:

  • 重试动作(重新调用WS)
  • 退出

这是到目前为止我的代码:

callPathsToMultiTiffWS(UID_KEY[9], stringCapturePaths, UID_KEY[1], UID_KEY[2], UID_KEY[3], UID_KEY[4], UID_KEY[5], UID_KEY[6]).then(
         function (complete) { 

          if (complete == true) {//if true, it stores the id of the picture to delete 
           Windows.UI.Popups.MessageDialog("WS executed successfully", "Info").showAsync().then(function (complete) {window.close();}); 
          } else { 
           var messageDialogPopup = new Windows.UI.Popups.MessageDialog("An error occur while calling WS, retry??", "Info"); 
           messageDialogPopup.commands.append(new Windows.UI.Popups.UICommand('Retry', function() { /*code for recall element*/ })); 
           messageDialogPopup.commands.append(new Windows.UI.Popups.UICommand('Exit', function() { /*code for exit*/ })); 
           messageDialogPopup.showAsync(); 

           _divInput.innerHTML = ""; 
          } 
         }, 
         function (error) { console.log("function error"); }); 

这工作不错,到目前为止,但我想召回功能工作 所以我想以使嵌入我的代码在这样一个循环内

  var ban = true; 
      while (true) { 

       callPathsToMultiTiffWS(UID_KEY[9], stringCapturePaths, UID_KEY[1], UID_KEY[2], UID_KEY[3], UID_KEY[4], UID_KEY[5], UID_KEY[6]).then(
        function (complete) { 
         if (complete == true) {//if true, it stores the id of the picture to delete 
          Windows.UI.Popups.MessageDialog("WS executed successfully", "Info").showAsync().then(function (complete) { window.close(); }); 
         } else { 
          var messageDialogPopup = new Windows.UI.Popups.MessageDialog("An error occur while calling WS, retry??", "Info"); 
          messageDialogPopup.commands.append(new Windows.UI.Popups.UICommand('Retry', function() { ban == true; })); 
          messageDialogPopup.commands.append(new Windows.UI.Popups.UICommand('Exit', function() { ban == false; })); 
          messageDialogPopup.showAsync().then(function (complete) { 
           console.log("no ps no"); 
          }); 
        } 
       }, 
       function (error) { console.log("function error"); }); 

       if (ban == false) break; 
      } 

此循环执行webService,但它不会等待用户交互来触发web服务触摸其中一个按钮,这是一个无休止的循环调用我的webService,如何解决这个问题?

先感谢您的支持

+0

什么是正是你正在努力实现与循环? –

+0

我想消费一个WebService,如果失败我想用一个重试按钮显示一个消息对话框,只有当用户接受时才重复所有的过程,但是我的循环重复这个过程而不显示消息对话框并且没有用户交互 – Jesus

回答

1

如果我不是失去了一些东西,它看起来像错误造成的,因为你的代码没有被设计的异步调用showAsync返回后运行接下来的任务。由于对showAsync的调用是非阻塞的,因此while循环将重新开始并再次调用Web服务。并且因为该呼叫(callPathsToMultiTiffWS)也是非阻塞的,所以该循环将再次从开始,触发对callPathsToMultiTiffWS的另一呼叫。再一次,再一次。

我的建议是下一个电话打破了Web服务这样,当用户进行选择,只会被触发。如果您将问题区分开来(将调用Web服务移至不同于通知用户问题的UI的模块),那么您可以修复此问题。

克雷格·布罗克舍密特有着极大的博客帖子大约承诺的更精细的细节: http://blogs.msdn.com/b/windowsappdev/archive/2013/06/11/all-about-promises-for-windows-store-apps-written-in-javascript.aspx

CNC中
下面是一些代码,我写信给试图证明你如何完成你想要什么:

function tryWebServiceCall(/* args */) { 
    var url = "some Web service URL"; 

    return new WinJS.xhr({ url: url }).then(
     function (complete) { 
      if (complete) { 
       return new Windows.UI.Popups.MessageDialog("WS executed successfully", "Info").showAsync().then(
        function() { /*do something */ }); 
      } else { 
       var messageDialogPopup = new Windows.UI.Popups.MessageDialog("An error occur while calling WS, retry??", "Info"); 
       messageDialogPopup.commands.append(new Windows.UI.Popups.UICommand('Retry', function() { 
        return tryWebServiceCall(/* args */); 
       })); 
       messageDialogPopup.commands.append(new Windows.UI.Popups.UICommand('Exit', function() { return; })); 
       return messageDialogPopup.showAsync(); 
      } 
     }); 
} 
+0

最好回答我可以让它工作得很好,谢谢! – Jesus