Skip to content

Instantly share code, notes, and snippets.

@scarfacedeb
Last active December 18, 2015 12:39
Show Gist options
  • Save scarfacedeb/5783961 to your computer and use it in GitHub Desktop.
Save scarfacedeb/5783961 to your computer and use it in GitHub Desktop.
jQuery plugin initialization example
$.fn.tabularize = function(options) {
if ( typeof options === 'string' ) {
var args = Array.prototype.slice.call( arguments, 1 );
this.each(function() {
var instance = $.data( this, 'tabularize' );
if ( !instance ) {
console.log( "cannot call methods on tabularize prior to initialization; " +
"attempted to call method '" + options + "'" );
return;
}
if ( !$.isFunction( instance[options] ) || options.charAt(0) === "_" ) {
console.log( "no such method '" + options + "' for tabularize instance" );
return;
}
instance[ options ].apply( instance, args );
});
} else {
this.each(function() {
var instance = $.data( this, 'tabularize' );
if ( instance ) {
instance._init();
} else {
instance = $.data( this, 'tabularize', new Tabularize( options, this ) );
}
});
}
return this;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment