Created
July 26, 2010 13:38
-
-
Save cowboy/490548 to your computer and use it in GitHub Desktop.
Another idea for setting jQuery plugin default options.
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
| // ============================ | |
| // $.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