2012-05-16 50 views
0
var Model= function() { 

     function GetData() {  
      // Sending the request and i am getting the response. 

      JsonClientScheduleCareProvider.onload = function() { 
       return this.responseText; 
      }; 
      // error handling 
      JsonClientScheduleCareProvider.onerror = function (e) { 

      }; 
     return { 
      GetApps: GetData, 
     } 

    }();  

在下面的代码中,我进行了JSON调用。如果我得到回复,我应该用回应调用sendData函数。JSON响应中的回调函数

var jsonData = Model.GetApps(); 
    if (!jsonData) { 
     Ti.API.warn("JsonData"); 
     SendData(jsonData); 
    } 

我现在面临的问题是jsonData得到我的回应前,SendData被调用。我只有在得到响应时才需要执行SendData函数。

回答

1

您需要等待,直到您的回复将被发送。为此使用callback函数。

尝试这样:

var Model= function() { 

    function GetData(callback) {  
     // Sending the request and i am getting the response. 

     JsonClientScheduleCareProvider.onload = function() { 
      callback(this.responseText); 
     }; 
     // error handling 
     JsonClientScheduleCareProvider.onerror = function (e) { 
      callback(null); 
     }; 
    } 
    return { 
     GetApps: GetData, 
    } 

}(); 

Model.GetApps(function(jsonData){ 
    if (!jsonData) { 
    Ti.API.warn("JsonData"); 
    SendData(jsonData); 
    } 
}); 
+0

这和OP似乎缺少一个'}'。 –

+0

@MattFenwick谢谢。已更新。 – Engineer