Skip to content

Instantly share code, notes, and snippets.

@cowboy
Created July 6, 2010 16:52
Show Gist options
  • Save cowboy/465622 to your computer and use it in GitHub Desktop.
Save cowboy/465622 to your computer and use it in GitHub Desktop.
Fillbox plugin, to go along with Rey Bango's blog post
// 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