Created
July 6, 2010 16:52
-
-
Save cowboy/465622 to your computer and use it in GitHub Desktop.
Fillbox plugin, to go along with Rey Bango's blog post
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Fillbox plugin, to go along with Rey Bango's blog post, | |
// "Generic Activity Indicator for Ajax Requests" | |
// http://blog.reybango.com/2010/07/06/generic-activity-indicator-for-ajax-requests/ | |
(function($){ | |
// Call as $('#foo').fillbox() to use default options or override as-needed | |
// like $('#bar').fillbox({ url: 'foo.php' }). | |
$.fn.fillbox = function( options ) { | |
// Options override defaults (but only if passed). | |
options = $.extend( {}, $.fn.fillbox.options, options ); | |
return this.each(function(){ | |
var elem = this; | |
$(elem) | |
// Remove any existing throbber. | |
.find( '.throbber' ) | |
.remove() | |
.end() | |
// Add a new throbber. | |
.prepend( '<img class="throbber" src="facebook.gif" />' ); | |
// Proxy callback methods so that `this` refers to this element. | |
$.each( [ 'complete', 'error', 'success' ], function(i,method){ | |
if ( options[ method ] ) { | |
options[ method ] = $.proxy( options[ method ], elem ); | |
} | |
}) | |
// Make the actual ajax call. | |
$.ajax( options ); | |
}); | |
}; | |
// Default options (changeable as $.fn.fillbox.options properties). | |
$.fn.fillbox.options = { | |
url: 'test.php', | |
cache: false, | |
success: function( data ) { | |
$(this).html( data ); | |
}, | |
complete: function() { | |
// A generic "remove the throbber" handler. | |
$(this).find( '.throbber' ).remove(); | |
} | |
}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment