Created
October 3, 2008 21:54
-
-
Save wycats/14660 to your computer and use it in GitHub Desktop.
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
/* | |
jQuery.plugin("resizable", { | |
options: { clickable: false }, | |
setup: function(){ | |
if ( this.options.clickable ) | |
// Automatically Namespaced | |
this.bind("click", function(){}); | |
this.addClass("test"); | |
}, | |
// Hidden: | |
// .unbind(".name") | |
// .removeData(".name") | |
// return false blocks it | |
teardown: function(){ | |
// Automatically Namespaced | |
if ( this.options.clickable ) | |
this.unbind("click"); | |
this.removeClass("test"); | |
}, | |
methods: { | |
blahResize: function(width){ | |
this.css("width", width); | |
} | |
} | |
}); | |
jQuery.plugin("another", function(name, address){ | |
this.options.name = name; | |
this.options.address = address; | |
if ( this.options.name ) | |
// Automatically Namespaced | |
this.bind("click", function(){}); | |
this.addClass("test"); | |
}); | |
jQuery("div") | |
.resiable({ clickable: true }) | |
.another(true, 50); | |
*/ | |
jQuery.plugin = function(name, fn, props){ | |
if ( typeof fn === "function" ) { | |
props = props || {}; | |
jQuery.fn[ props.name || name ] = function(){ | |
var ret, args = arguments; | |
this.each(function(){ | |
var instance = jQuery.data( this, name ); | |
if ( !instance ) { | |
instance = jQuery(this); | |
instance.options = props.options ? | |
jQuery.extend({}, props.options, args[0]) : {}; | |
for( prop in jQuery.fn[ props.name || name ] ) { | |
jQuery.fn[ props.name || name ] ).apply( this, instance ) | |
} | |
jQuery.data( this, name, instance ); | |
jQuery(this).bind("remove." + name, function(){ | |
if ( props.teardown ) { | |
props.teardown.call( jQuery.data( this, name ) ); | |
} | |
jQuery(this) | |
.removeData(name) | |
.removeData("." + name) | |
.unbind("." + name); | |
}).bind("setData." + name, function(e, key, value){ | |
jQuery.data( this, name ).options[key] = value; | |
}).bind("getData." + name, function(e, key){ | |
return jQuery.data( this, name ).options[key]; | |
}); | |
} | |
ret = fn.apply( instance, args ); | |
}); | |
return typeof ret === "undefined" ? this : ret; | |
}; | |
} else { | |
jQuery.plugin(name, fn.setup, fn); | |
if ( fn.methods ) { | |
for ( var method in fn.methods ) { | |
fn.name = method; | |
jQuery.plugin(name, fn.methods[method], fn); | |
} | |
} | |
} | |
}; | |
jQuery.plugin.addOption = function( pluginName, option, fn ) { | |
jQuery.fn[pluginName][option] = fn; | |
} | |
jQuery.fn.option = function(plugin, name, value){ | |
return this.data(name + "." + plugin, value); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment