jQuery confirmation dialog with bootstrap

Ive been using Twitters Bootstrap for putting together a web UI for our UAV design system (that will be partially opened up to the general public at some point).  jQuery also plays a role in this and at some point I needed an easy confirmation dialog.  I could easily have used jQueryUI as I have in the past, but I found this great plugin by Damian Antipa that does exactly what I needed & uses just jQuery and Bootstrap.

The only thing I needed to change was that I wanted the confirmation dialog (on ok) to trigger the original click() handle instead of redirecting to a different URL.  My basic patch is:

@@ -52,6 +52,17 @@
                                var o = options;
                                var $elem = $(this)
 
+                               //is there an existing click handler registered
+                               if ($elem.data('events') && $elem.data('events').click) {
+                                       //save the handler (TODO: assumes only one)
+                                       var targetClickFun = $elem.data('events').click[0].handler;
+                                       //unbind it to prevent it firing
+                                       $elem.unbind('click');
+                               }else{
+                                       //assume there is a href attribute to redirect to
+                                       var targetClickFun = function() {window.location.href = $elem.attr('href');};
+                               }
+
                                $elem.bind('click', function(e) {
                                        e.preventDefault();
                                        if(!$('#confirm-dialog').length) {
@@ -89,7 +100,8 @@
                                                $dialog.find('p.message').html(o.message);
 
                                                $dialog.find('a.btn:eq(0)').text($elem.text()).bind('click', function(e) {
-                                                       window.location.href = $elem.attr('href');
+                                                       $dialog.remove();
+                                                       targetClickFun();
                                                });
 
                                                $dialog.find('a.btn:eq(1)').text(o.cancelButton).bind('click', function(e) {

Cleaner would be to add some options and what have you.  And maybe I will come to that.  For now I put the full code in a public gist on github.  So fork ahead 🙂

–Dirk