File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change @@ -91,6 +91,41 @@ Notice that we return the results of `each()` instead of returning `this`.
91
91
Since ` each() ` is already chainable, it returns ` this ` , which we then return.
92
92
This is a better way to maintain chainability than what we've been doing so far.
93
93
94
+ ##Accepting options
95
+
96
+ As your plugins get more and more complex, it's a good idea to make your plugin
97
+ customizable by accepting options. The easiest way do this, especially if there
98
+ are lots of options, is with an object literal. Let's change our greenify plugin to
99
+ accept some options.
100
+
101
+ <javascript >
102
+ (function ($) {
103
+ $.greenify = function (options) {
104
+ // This is the easiest way to have default options.
105
+ var settings = $.extend( {
106
+ 'color' : '#556B2F', // These are the defaults
107
+ 'background-color' : 'white'
108
+ }, options);
109
+
110
+ // Greenify the collection based on the settings variable
111
+ return this.css({
112
+ 'color': settings['color'],
113
+ 'background-color': settings['background-color']
114
+ });
115
+ };
116
+ }(jQuery));
117
+ </javascript >
118
+
119
+ Example usage:
120
+
121
+ <javascript >
122
+ $('div').greenify({
123
+ 'color': 'orange'
124
+ });
125
+ </javascript >
126
+
127
+ The default value for ` color ` of ` #556B2F ` gets overriden by ` $.extend ` to be orange.
128
+
94
129
##Putting it together
95
130
96
131
Here's an example of a small plugin using some of the techniques
You can’t perform that action at this time.
0 commit comments