2012-03-10 51 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”,当用户点击按钮或者将被调用。该函数将接收一个布尔参数,告诉是否点击确定或取消。