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, 
     } 

    }();  

Dans le code ci-dessous, je fais mon appel JSON. Si j'obtiens la réponse, je devrais appeler la fonction sendData avec la réponse.Fonction de rappel dans la réponse JSON

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

Le problème que je suis confronté est avant la jsonData me reçoit la réponse, le SendData est appelé. Je dois exécuter la fonction SendData seulement quand j'obtiens la réponse.

Répondre

1

Vous devez attendre jusqu'à ce que votre réponse soit livrée. Utilisez pour cela la fonction callback.

Essayez quelque chose comme ceci:

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

Ceci et l'OP semblent manquer un ''}. –

+0

@MattFenwick Merci. Déjà mis à jour. – Engineer