2016-05-13 76 views
0

我使用科爾多瓦創建我的移動應用程序。當我從sqlite數據庫獲取數據需要一些時間,這就是爲什麼我使用延遲jQuery。但是我無法從$。當推遲jQuery時調用函數。我很困惑。請幫幫我。jQuery異步函數不叫

callPaymentApi: function (e) { 
    var loginDeferred = $.Deferred(); 
    var loginList = []; 
    var dbLogin = new DBLogin(); 
    dbLogin.getLoginDetail(function (data) { 
     loginList = data; 
     loginDeferred.resolve(); 
    }); 
    $.when(loginDeferred).then(function success() { 
     var email_id = loginList[0].email_id; 
     var token_id = loginList[0].token_id; 
     currTime = MyApp.getRequestTime(); 

     var data = {}; 
     data.request_time = currTime; 
     data.token_id = token_id; 
     data.email_id = email_id; 
     console.log("data :::::::::: " + JSON.stringify(data)); 

     var net = MyApp.checkNetworkConnection(); 
     if (net == true) { 
      $.blockUI(); 
      var urlRoot = String.baseURL + 'fetchdata'; 
      var loginModel = new Model(); 
      loginModel.urlRoot = urlRoot; 
      loginModel.save(data, { 
       success: function (response) { 
        $.unblockUI(); 
        console.log("success ....." + JSON.stringify(response)); 
        this.successHandler(response); 
       }, 
       error: function (response) { 
        $.unblockUI(); 
        console.log("error ....." + JSON.stringify(response)); 
        this.errorHandler(response); 
       } 
      }); 
     } else { 
      navigator.notification.alert(l('%alert.internet.connection'), null, null, l('%common.OK')); 
     } 
    }); 
}, 
successHandler: function (response) { //Success handler 
    $.unblockUI(); 
    alert("success"); 
}, 
errorHandler: function (response) { //Success handler 
    $.unblockUI(); 
    alert("success"); 
} 

我得到了日誌中的數據。 REST API也工作正常,並獲得成功的迴應。但是當我從成功和錯誤中調用函數時,它不起作用。我無法調用這些函數「successHandler」&「errorHandler」。

在此先感謝!

回答

0

回調中的上下文會有所不同。您需要保存對當前上下文的引用,如:

callPaymentApi: function(e) { 
    var that = this; 
    ... 
    $.when(loginDeferred).then(function success() { 
     ... 
     loginModel.save(data, { 
     success: function(response) { 
      ... 
      that.successHandler(response); 
     }, 
     error: function(response) { 
      ... 
      that.errorHandler(response); 
     } 
     }); 
    }); 
    }, 
    successHandler: function(response) { //Success handler 
    $.unblockUI(); 
    alert("success"); 
    }, 
    errorHandler: function(response) { //Success handler 
    $.unblockUI(); 
    alert("success"); 
    } 
+0

感謝您的回覆。這對我很有幫助。 – Krishna