Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I return true or false using jQuery noty plugin?

I have the following jQuery using the noty plugin and plum shopping cart jQuery. The first block of code correctly alerts a yes / no and empties the cart correctly.

The second block of code shows the yes / no message correctly using noty BUT it does not return the true / false so the cart isnt emptied.

Im really new to jQuery so I'm probably missing something obvious ! Any help would be appreciated:

//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;
}, 
like image 873
Luke Bream Avatar asked May 17 '26 21:05

Luke Bream


1 Answers

Well, the plugin is receiving callbacks, so when you use callbacks you must think in a asynchronous way.

/**
 * @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);
}

Above, you will send a callbackfunction "onConfirm" which will be called when the user clicks on either buttons. The function will receive one boolean argument telling if was a click on Ok or Cancel.

like image 114
Rubens Pinheiro Avatar answered May 20 '26 09:05

Rubens Pinheiro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!