李允智
李允智

Reputation: 95

how to return true or false when i code window.confirm myself

Code:

 (function (proxied) {
    window.confirm = function (msg) {
        noty({
            text: '<i class="fa fa-exclamation-triangle">'+msg+'</i>',
            theme: 'relax',
            dismissQueue: true,
            layout: 'center',
            buttons: [
                {
                    addClass: 'btn btn-success btn-circle', text: '<i class="fa fa-check"></i>', onClick: function ($noty) {
                        $noty.close();
                        return true;
                    }
                },
                {
                    addClass: 'btn btn-danger btn-circle', text: '<i class="fa fa-times"></i>', onClick: function ($noty) {
                        $noty.close();
                        return false;
                    }
                }
            ]
        });
        //return proxied.apply(this, arguments);
    };
})(window.confirm);

It can not correct return true or false, I guess it may be the buttons closures ?Thanks everyone.

Upvotes: 0

Views: 834

Answers (1)

metarmask
metarmask

Reputation: 1857

Only the native dialog functions can pause JavaScript execution until some action is taken which means you won't be able to return anything. You will have to use callbacks:

(function (proxied) {
    window.confirm = function (msg, callback) {
        noty({
            text: '<i class="fa fa-exclamation-triangle">'+msg+'</i>',
            theme: 'relax',
            dismissQueue: true,
            layout: 'center',
            buttons: [
                {
                    addClass: 'btn btn-success btn-circle', text: '<i class="fa fa-check"></i>', onClick: function ($noty) {
                        $noty.close();
                        callback(true);
                    }
                },
                {
                    addClass: 'btn btn-danger btn-circle', text: '<i class="fa fa-times"></i>', onClick: function ($noty) {
                        $noty.close();
                        callback(false);
                    }
                }
            ]
        });
        //return proxied.apply(this, arguments);
    };
})(window.confirm);

To use your function you will then have to do this:

window.confirm("Do you want ice cream?", function(result){
    if(result){
        // The user wants ice cream
    }
});

Upvotes: 1

Related Questions