开发者

jQueryUI modal add function to defaults

I always find myself fixing this chrome scro开发者_如何学Gollbar issue on every jQueryUI modal I add. So, I was thinking that I could add that code to the jqueryUI dialog defaults. I found this question but can't quite bridge the gap and apply it to my code.

$.extend($.ui.dialog.prototype.options, {
    open: function(event, ui){
        window.setTimeout(function(){jQuery(document).unbind('mousedown.dialog-overlay').unbind('mouseup.dialog-overlay');}, 100);
    }
});

The problem is, if I have code in a dialog open event, it will override the default.

$("#modalContent").dialog({
    open:function(){
        alert('Hey buddy!');
    }
});

So my question is two parts; How do I stack the functions instead of overriding them and should I even do this in the first place? Thanks!

Here is a jsfiddle: http://jsfiddle.net/McWatt/HSA8M/


The solution is alot simpler then that. You can just remove those events by overriding $.ui.dialog.overlay.events, which will prevent them from being bound to begin with:

$.ui.dialog.overlay.events = $.map('focus,keydown,keypress'.split(','),
        function(event) { return event + '.dialog-overlay'; }).join(' ');

Note: If you remove keydown you won't be able to close the dialog with the escape key. You might want to look at overriding the create-method as well. Check the source for more info.

See test case on jsFiddle


In my opinion you have one problem in your jsfiddle:

  • the dialog is not destroyed and therefore the open callback remains the same

Here is an updated fiddle: http://jsfiddle.net/thomas_peklak/HSA8M/3/

Still, I would not really recommend modifying the defaults of a library object. In my opinion it is a lot safer to create your own ui.widget based on ui.dialog and override the defaults in your object.

Your case seems to be rather simple but where should we draw the line between clean and entangled code?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜