Skip to content

Instantly share code, notes, and snippets.

@cowboy
Created July 26, 2010 13:38
Show Gist options
  • Select an option

  • Save cowboy/490548 to your computer and use it in GitHub Desktop.

Select an option

Save cowboy/490548 to your computer and use it in GitHub Desktop.
Another idea for setting jQuery plugin default options.
// ============================
// $.myMethod-based plugin
// ============================
(function($){
var ns = $.myMethod = function( options ) {
options = $.extend( {}, ns.options, options );
// Awesome code goes here
console.log( options );
};
ns.options = {
myProp1: 'foo',
myProp2: 'bar'
};
})(jQuery);
// Changing defaults in user code:
$.myMethod.options.myProp1 = 'something different';
// Or:
$.myMethod.options = {
myProp1: 'omg',
myProp2: 'ponies'
};
// ============================
// $.myMethod-based plugin
// ============================
(function($){
var ns = $.fn.myMethod = function( options ) {
options = $.extend( {}, ns.options, options );
return this.each(function(){
// Awesome code goes here.
console.log( options );
})
};
ns.options = {
myProp1: 'foo',
myProp2: 'bar'
};
})(jQuery);
// Changing defaults in user code:
$.fn.myMethod.options.myProp1 = 'something different';
// Or:
$.fn.myMethod.options = {
myProp1: 'omg',
myProp2: 'ponies'
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment