Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Plugin On Event Return True/False

I'm creating a plugin that replaces alerts/confirms for a project and I was curious if there was a way to make it like a real confirm where you can do:

if(confirm('Yes or no?')){alert('You agreed!');}

Right now I could do with a call back using this syntax:

$.alert('yes or no',{type:'confirm'});

But i want to be able to do:

if($.alert('yes or no',{type:'confirm'})){/*some action on true*/}

Here is what I have so far and look for the all CAPS comments in the click event (remember, this is still in development, so the HTML and stuff is still a little icky):

(function($) {
    $.alert = function(message,options) {
        defaults = {
            type:'alert',
            callback:function(){}
        }
        options = $.extend({},defaults,options);
        if(options.type == 'confirm'){
            $('<div style="display:none" class="alerthiddenoverlay"></div><div style="display:none" class="customalertbox"><div><img src="http://cdn.iconfinder.net/data/icons/basicset/tick_48.png"><p>'+message+'</p><br class="clear"><span><a class="cancel" href="#cancel">Cancel</a><a class="ok" href="#ok">OK</a></span><br class="clear"></div></div>').prependTo('body');
        }
        else{
            $('<div style="display:none" class="alerthiddenoverlay"></div><div style="display:none" class="customalertbox"><div><img src="http://cdn.iconfinder.net/data/icons/basicset/warning_48.png"><p>'+message+'</p><br class="clear"><span><a class="ok" href="#ok">OK</a></span><br class="clear"></div></div>').prependTo('body');
        }
        $alertboxclass=$('.customalertbox');
        $alerthiddenoverlay=$('.alerthiddenoverlay');
        $alertboxclass.find('a').click(function(event){
            event.preventDefault();
            var the_return = false;
            if($(this).attr('href') == '#ok'){
                var the_return = true;
            }
            $alertboxclass.fadeOut(250,function(){$alerthiddenoverlay.delay(250).fadeOut(250,function(){$(this).remove();options.callback.call(this,the_return);});$(this).remove()});
        });
        $alertboxclass.css({
            top:$(window).height()/2-$alertboxclass.height()/2,
            left:$(window).width()/2-$alertboxclass.width()/2
        });
        $alerthiddenoverlay.css({height:$(window).height()+'px',width:'100%',position:'fixed',zIndex:'9998'}).fadeIn(250,function(){$alertboxclass.delay(250).fadeIn()});
    }
})(jQuery);
like image 888
Oscar Godson Avatar asked Mar 10 '26 18:03

Oscar Godson


1 Answers

I think passing a callback method in as a parameter to the $.alert function is going to be the easiest option. If that's a deal-breaker though, I might look at the .queue() method for chaining the events.

http://api.jquery.com/queue/

like image 76
nleach Avatar answered Mar 12 '26 08:03

nleach



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!