2012-03-10 42 views
3

我有以下jQuery使用noty插件和梅花購物車jQuery。第一個代碼塊正確地提醒是/否並正確清空購物車。如何使用jQuery noty插件返回true或false?

第二個代碼塊顯示yes/no消息正確使用noty但它不返回true/false,所以購物車沒有清空。

我真的很新的jQuery,所以我可能會錯過一些明顯的東西!任何幫助,將不勝感激:

//This works.. 
emptycart: function() { 
    if (!confirm('Are you sure you want to empty your cart?')) { 
     return false; 
    } 
}, 

//This doesnt work.. 

emptycart: function() { 
    confirm.call(this, noty({ 
     text: 'Are you sure you want to empty the cart ?', 
     buttons: [ 
      {type: 'button green', text: 'Ok', click: function() { return false; } }, 
      {type: 'button pink', text: 'Cancel', click: function() { return true; } } 
     ], 
     closable: false, 
     timeout: false 
     }), 
     true 
    ); 
    return false; 
}, 
+0

你可以做一個jsfiddle嗎? – MCSI 2012-08-27 13:28:34

回答

1

那麼,該插件接收回調,所以當你使用回調你必須考慮以異步方式。

/** 
* @param {function} onConfirm : Receives true or false as argument, depending on the user choice 
*/ 
emptycart: function (onConfirm) { 
    confirm.call(this, noty({ 
     text: 'Are you sure you want to empty the cart ?', 
     buttons: [ 
      { 
       type: 'button green', 
       text: 'Ok', 
       click: function() { 
        //Do other stuff if you want... 
        onConfirm(true); 
       } 
      }, 
      { 
       type: 'button pink', 
       text: 'Cancel', 
       click: function() { 
        //Do other stuff if you want... 
        onConfirm(false); 
       } 
      } 
     ], 
     closable: false, 
     timeout: false 
    }), true); 
} 

上面,你會發送一個callbackFunction參數「onConfirm」,當用戶點擊按鈕或者將被調用。該函數將接收一個布爾參數,告訴是否點擊確定或取消。