From 9af1c8391d8a1f99c79273aa3e9f38232979141f Mon Sep 17 00:00:00 2001 From: Adrian Olaru Date: Fri, 20 Apr 2012 00:52:42 +0300 Subject: [PATCH] Added spinner custom events Named them SHOW_SPINNER and HIDE_SPINNER. Cached body selection as a side effect. --- examples/index.html | 2 ++ jquery.modal.js | 16 ++++++++++------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/examples/index.html b/examples/index.html index e41c861..61ace12 100644 --- a/examples/index.html +++ b/examples/index.html @@ -178,6 +178,8 @@

Example 5: the un-closable window

$(document).on($.modal.OPEN, log_modal_event); $(document).on($.modal.BEFORE_CLOSE, log_modal_event); $(document).on($.modal.CLOSE, log_modal_event); + $(document).on($.modal.SHOW_SPINNER, log_modal_event); + $(document).on($.modal.HIDE_SPINNER, log_modal_event); $('#more').click(function() { $(this).parent().after($(this).parent().next().clone()); diff --git a/jquery.modal.js b/jquery.modal.js index d72679b..eb45b95 100644 --- a/jquery.modal.js +++ b/jquery.modal.js @@ -8,6 +8,7 @@ $.modal = function(el, options) { var remove, target; + this.$body = $('body'); this.options = $.extend({}, $.modal.defaults, options); if (el.is('a')) { target = el.attr('href'); @@ -19,16 +20,15 @@ //AJAX } else { this.$elm = $('
'); + this.$body.append(this.$elm); remove = function(event, modal) { modal.elm.remove(); }; this.showSpinner(); $.get(target).done(function(html) { if (!current) return; - current.$elm.empty().append(html).appendTo('body').on($.modal.CLOSE, remove); + current.$elm.empty().append(html).on($.modal.CLOSE, remove); current.hideSpinner(); current.open(); - }).fail(function(error) { - current.hideSpinner(); - }); + }).fail(function() { current.hideSpinner(); }); } } else { this.$elm = el; @@ -66,7 +66,7 @@ background: this.options.overlay, opacity: this.options.opacity }); - $('body').append(this.blocker); + this.$body.append(this.blocker); this.$elm.trigger($.modal.BLOCK, [this._ctx()]); }, @@ -93,14 +93,16 @@ }, showSpinner: function() { + this.$elm.trigger($.modal.SHOW_SPINNER, [this._ctx()]); if (!this.options.showSpinner) return; this.spinner = $('
') .append(this.options.spinnerHtml); - $('body').append(this.spinner); + this.$body.append(this.spinner); this.spinner.show(); }, hideSpinner: function() { + this.$elm.trigger($.modal.HIDE_SPINNER, [this._ctx()]); if (this.spinner) this.spinner.fadeOut(); }, @@ -156,6 +158,8 @@ $.modal.OPEN = 'modal:open'; $.modal.BEFORE_CLOSE = 'modal:before-close'; $.modal.CLOSE = 'modal:close'; + $.modal.SHOW_SPINNER = 'modal:show-spinner'; + $.modal.HIDE_SPINNER = 'modal:hide-spinner'; $.fn.modal = function(options){ if (this.length === 1) {