View Full Version : FYI: a subtle "gotcha" with grid plugins

Mike Robinson
8 Feb 2010, 8:26 AM
Here's a "gotcha" that bears repeating.

ExtJS has a very nice "plugins" system that lets you extend the behavior of various stock objects by "plugging in" auxiliary code objects. One of the most common places where this concept is used is: grids.

The items listed in the plugins property (an array) can be variables or they can be strings. If the element is a string, ExtJS will automatically create an instance of the necessary object, based on the string you provided. (See also: ptype.)

But it is frequently the case, with grids, that the plugin object is referred to twice: once in the plugins list; a second time in columns.

Any time that you do this, it is critical to know that you are referring to the same object-instance.

Which means that you must not use a character-string in either case. You must instantiate the plugin first, assigning it to a var, and then refer to that same var in both places. Only in this way will the grid properly refer, twice, to the same software object instance.

What goes wrong when you use character strings? Well, each time ExtJS encounters that string, it instantiates a new object-instance each time. So, you've got two object instances. The plugin object instance (#1) that is associated with the plugins list (and which therefore got correctly initialized by a timely call to its "init" method) ... is not the object instance (#2) which appears in the columns list (which was never correctly initialized). Neither object-instance knows that the other one even exists.

And when that happens ... woo hoo! ... what a wonderful :((way :(( to spend a day!

P.S.: Do not carry this any farther. When two or more ExtJS objects need the services of "a plugin," each of those objects should receive their own distinct instance of that plugin; one per object. If you only need one connection to the plugin, just use the plugins-list mechanism. (That's what it's for.) When you need to make multiple references to the same plugin object within one object, instead use the procedure described here.