2014-10-08 99 views
0

所以我對這些承諾很新。我很難在他們周圍纏着我的頭。我看了很多內容和SO帖子,但沒有遇到類似我的情況。 這是我目前的代碼。使用jQuery推遲承諾

$("#PayNow, input[name^='schedulepmt']").on("blur", function(){ 
var diff = 0; 
diff = calcBalanceDue(); 
if(diff){ 
    $.when(confirmNewPayment(diff)).then(function(){ 
     if($(this).attr("id") === "PayNow" && input[name^='schedulepmt'].length && ($(this).val() !== $(this).data("prevValue"))){ 
      if(!resetPmtInfo()) { 
       $(this).val($(this).data("prevValue")); 
      } 
     } 
    }); 
    } 
}); 

我想.then()裏面的代碼confirmNewPayment()已執行後執行。它似乎仍然在運行異步。

謝謝。

這裏是confirmNewPayment()

function confirmNewPayment(diff){ 
bootbox.dialog({ 
    message: "There is an outstanding balance. How would you like to proceed?", 
    title: "Outstanding Balance Options", 
    closeButton: false, 
    buttons: { 
     addpmt: { 
      label: "Add a scheduled CC payment", 
      className: "btn-success", 
      callback: function() { 
       scheduledPmtData.PaymentCount += 1; 
       $("#ScheduledPayments").append($("#ScheduledPmtTemplate").render(scheduledPmtData,{amount:diff})); 
      } 
     }, 
     collect: { 
      label: "Mark balance as \"To Collect\"", 
      className: "btn-primary", 
      callback: function() { 
       $("#BalanceDueRow").removeClass("hide"); 
       $("#BalanceDue").val(diff); 
      } 
     } 
    } 
    }); 
} 
+1

是什麼'confirmNewPayment'樣子?你爲什麼要調用'$ .when'呢?它是否會返回一個承諾? – 2014-10-08 19:52:18

+0

我加了fn。不,它不會返回任何東西。 – dbinott 2014-10-08 19:55:42

+2

這就是爲什麼這不像你期望的那樣工作。你爲什麼想在這裏使用承諾? confirmNewPayment'的哪一部分是異步的?你想什麼時候運行'.then'?對話框打開後?點擊一個按鈕後? – 2014-10-08 19:56:18

回答

2

你confirmNewPayment函數返回一個承諾:

function confirmNewPayment(diff){ 
    var deferred = $.Deferred(); 

    // Your async action here 
    (function() { 
     // $when will run your function when the deferred object is resolved 
     deferred.resolve(diff * 2); 
    })(); 

    return deferred.promise(); 
} 

對於更新:

一切會更容易些,如果bootbox返航一個承諾通過設計。因此,在這種情況下,你可以做

function confirmNewPayment(diff){ 
    var deferred = $.Deferred(); 

    bootbox.dialog({ 
     message: "There is an outstanding balance. How would you like to proceed?", 
     title: "Outstanding Balance Options", 
     closeButton: false, 
     buttons: { 
      addpmt: { 
       label: "Add a scheduled CC payment", 
       className: "btn-success", 
       callback: function() { 
        scheduledPmtData.PaymentCount += 1; 
        $("#ScheduledPayments").append($("#ScheduledPmtTemplate").render(scheduledPmtData,{amount:diff})); 

        deferred.resolve('schedule'); 
       } 
      }, 
      collect: { 
       label: "Mark balance as \"To Collect\"", 
       className: "btn-primary", 
       callback: function() { 
        $("#BalanceDueRow").removeClass("hide"); 
        $("#BalanceDue").val(diff); 

        deferred.resolve('mark'); 
       } 
      } 
     } 
    }); 

    return deferred.promise(); 
} 

...

$("#PayNow, input[name^='schedulepmt']").on("blur", function(){ 
    .... 
    confirmNewPayment(diff).then(function(selection) { 
     // selection is 'schedule' or 'mark' 
    }); 
} 
+2

您不需要'$ .when'。你可以'confirmNewPayment(diff).then(function(){});' – 2014-10-08 19:58:16

+0

是的,他並不需要'$ .when'在這種情況下...... – madpoet 2014-10-08 20:00:33

+0

我的代碼應該放在'(function (){'或內部? – dbinott 2014-10-08 20:07:13