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

이과 영업 이익이 보인다는'}'누락 될 수 있습니다. –

+0

@MattFenwick 감사합니다. 이미 업데이트되었습니다. – Engineer