Skip to content

Commit

Permalink
Fix for the potential semantic confusion between settings, defaults, …
Browse files Browse the repository at this point in the history
…and options.

Prior to this change, these two files used settings, something called "defaultOptions", and options.

Using "defaultOptions" is problematic because it does not cleanly separate defaults and options.  It needlessly confounds the two.

The `namespaced-starter.html` file actually fell into this trap.  See the comment on line 36 here: chuanxshi@7f3d3d3#jquery-plugin-patterns/namespaced-starter.html

Besides "defaults" is a better term than "defaultOptions".  This may matter for non-English native readers.

THEREFORE
options: values provided by the plugin's calling code.
defaults: the settings provided  A-Priori by the plugin
settings: the final, amalgamated run-time spec for the plugin

An additional benefit: cleanly separating options, defaults, and settings makes these available for subsequent runs of the plugin without mutating the plugin's defaults, which can be restored at anytime without redefining the plugin.  This is not so much a problem for these two examples, but there are other jQuery plugin patterns in this collection where this definitely applies.

For example, https://github.com/shichuan/javascript-patterns/blob/master/jquery-plugin-patterns/best-options.html where "options" is (for the moment) used in lieu of settings, thus destroying the user-provided options object.  I'll fix that separately.

Signed-off-by: Steven Black <[email protected]>
  • Loading branch information
StevenBlack committed Mar 27, 2012
1 parent d34a297 commit a506ff3
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions jquery-plugin-patterns/extend.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
$.fn.extend( {
pluginname: function( options ) {

this.defaultOptions = {};
this.defaults = {};

var settings = $.extend( {}, this.defaultOptions, options );
var settings = $.extend( {}, this.defaults, options );

return this.each( function() {

Expand Down
4 changes: 2 additions & 2 deletions jquery-plugin-patterns/namespaced-starter.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
base.init = function () {
base.myFunctionParam = myFunctionParam;

base.options = $.extend( {}, $.myNamespace.myPluginName.defaultOptions, options );
base.settings = $.extend( {}, $.myNamespace.myPluginName.defaults, options );

// Put your initialization code here

Expand All @@ -49,7 +49,7 @@
base.init();
};

$.myNamespace.myPluginName.defaultOptions = {
$.myNamespace.myPluginName.defaults = {
myDefaultValue: ""
};

Expand Down

0 comments on commit a506ff3

Please sign in to comment.