2012-05-16 88 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