Skip to content
This repository

Fixes getter methods created when plugins are initialized. #20

Merged
merged 1 commit into from about 2 years ago

2 participants

Jason Daly Alex Gorbatchev
Jason Daly

Because the following function definition is not wrapped in a closure

self[name] = function() { return plugin; };

plugin's scope is the TextExt.initPlugins function. Because of this,
on each iteration of the list of plugins to create the getter methods in
TextExt, the return value of all created methods will be the last
value of plugin and not the value of plugin from when the function
was defined for each.

Wrapping the function definition in a closure fixes this issue.

As an explicit example:

Previously, when instantiating a new textext(...) instance and
defining the plugins option as

plugins: 'autocomplete arrow'

All 3 of the following would return the TextExtArrow instance

$('...').textext()[0].ie9();
$('...').textext()[0].autocomplete();
$('...').textext()[0].arrow();

After they closure from this changeset is applied, they return their
expected plugin instances

 $('...').textext()[0].ie9();          // TextExtIE9Patches
 $('...').textext()[0].autocomplete(); // TextExtAutocomplete
 $('...').textext()[0].arrow();        // TextExtArrow
Fixes getter methods created when plugins are initialized.
Because the following function definition is not wrapped in a closure

    self[name] = function() { return plugin; };

`plugin`'s scope is the `TextExt.initPlugins` function. Because of this,
on each iteration of the list of plugins to create the getter methods in
`TextExt`, the return value of all created methods will be the last
value of `plugin` and not the value of `plugin` from when the function
was defined for each.

Wrapping the function definition in a closure fixes this issue.

As an explicit example:

Previously, when instantiating a new `textext(...)` instance and
defining the `plugins` option as

    plugins: 'autocomplete arrow'

All 3 of the following would return the `TextExtArrow` instance

    $('...').textext()[0].ie9();
    $('...').textext()[0].autocomplete();
    $('...').textext()[0].arrow();

After they closure from this changeset is applied, they return their
expected plugin instances

     $('...').textext()[0].ie9();          // TextExtIE9Patches
     $('...').textext()[0].autocomplete(); // TextExtAutocomplete
     $('...').textext()[0].arrow();        // TextExtArrow
0a805ee
Alex Gorbatchev alexgorbatchev merged commit f7b74dd into from February 19, 2012
Alex Gorbatchev alexgorbatchev closed this February 19, 2012
Alex Gorbatchev

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Feb 07, 2012
Fixes getter methods created when plugins are initialized.
Because the following function definition is not wrapped in a closure

    self[name] = function() { return plugin; };

`plugin`'s scope is the `TextExt.initPlugins` function. Because of this,
on each iteration of the list of plugins to create the getter methods in
`TextExt`, the return value of all created methods will be the last
value of `plugin` and not the value of `plugin` from when the function
was defined for each.

Wrapping the function definition in a closure fixes this issue.

As an explicit example:

Previously, when instantiating a new `textext(...)` instance and
defining the `plugins` option as

    plugins: 'autocomplete arrow'

All 3 of the following would return the `TextExtArrow` instance

    $('...').textext()[0].ie9();
    $('...').textext()[0].autocomplete();
    $('...').textext()[0].arrow();

After they closure from this changeset is applied, they return their
expected plugin instances

     $('...').textext()[0].ie9();          // TextExtIE9Patches
     $('...').textext()[0].autocomplete(); // TextExtAutocomplete
     $('...').textext()[0].arrow();        // TextExtArrow
0a805ee
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 3 additions and 1 deletion. Show diff stats Hide diff stats

  1. 4  src/js/textext.core.js
4  src/js/textext.core.js
@@ -802,7 +802,9 @@
802 802
 			if(plugin)
803 803
 			{
804 804
 				self._plugins[name] = plugin = new plugin();
805  
-				self[name] = function() { return plugin; };
  805
+				self[name] = (function(plugin) { 
  806
+				  return function(){ return plugin; } 
  807
+				})(plugin);
806 808
 				initList.push(plugin);
807 809
 				$.extend(true, plugin, self.opts(OPT_EXT + '.*'), self.opts(OPT_EXT + '.' + name));
808 810
 			}
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.