How to return true or false using jQuery noty plugin?

I have the following jQuery, using noty loading cart and jQuery draining cart. The first block of code correctly warns of yes / no and empties the cart correctly.

The second block of code shows the yes / no message correctly using noty, but it doesn't return true / false, so the trash is not emptied.

I'm really new to jQuery, so I'm probably missing something obvious! Any help would be greatly 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;
}, 

      

+3


source to share


1 answer


Well, the plugin receives callbacks, so when you use callbacks you have to think in an 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 dispatch the "onConfirm" callback function that will be called when the user clicks on any of the buttons. The function will receive one boolean argument indicating whether OK or Cancel was clicked.

+1


source







All Articles