PDA

View Full Version : How can I tell if a grid has plugins?



castitas
5 Oct 2012, 10:32 AM
I have button that serves two grids. In the handler, it has this:



if(button.up('grid').getPlugin('roweditor').editing) {
...

One grid has plugins, and it works fine. The other throws "plugins is undefined". I could use


var grid = button.up('grid');
if(grid.plugins && grid.getPlugin('roweditor').editing) {
...

but that depends on the plugins attribute. Can anyone think of a better way?

skirtle
5 Oct 2012, 11:42 AM
If calling getPlugin causes an error to be thrown I'd say it's a bug. Your workaround looks sensible but it'd probably be worth filing it in the bugs forum to get it fixed properly.

scottmartin
5 Oct 2012, 2:57 PM
You should be able to call without error. Is this present in a older/newer release?



pluginId: 'myPluginId',
grid.getPlugin('myPluginId');


Scott.

skirtle
5 Oct 2012, 3:18 PM
@scottmartin. I believe the problem being observed is that an error is thrown if the component doesn't have any plugins. For consistency (and common sense) it should just return undefined if the plugin id isn't found.

scottmartin
5 Oct 2012, 3:41 PM
Understood... this seems to work:



var myPlugin = grid.getPlugin('IdoNoExist');
if (myPlugin === undefined) { console.log('undefined'); }


Scott.

ojintoad
6 Feb 2013, 8:15 PM
A developer on my team thinks this is a bug and I agree with him. I'll make his case below: I just created this JSFiddle http://jsfiddle.net/LMaaM/.

This is based on 4.1.1 (not 4.1.3) which is latest. The test is obviously pretty contrived. However, I think it still illustrates the bug since in one case it works fine and in the other case it fails. This has to do with the fact that the code for the getPlugins code in Ext.AbstractComponent (http://docs.sencha.com/ext-js/4-1/source/AbstractComponent.html#Ext-AbstractComponent-method-getPlugin) is:



/**
* Retrieves a plugin by its `pluginId` which has been bound to this component.
* @param {String} pluginId
* @return {Ext.AbstractPlugin} plugin instance.
*/
getPlugin: function(pluginId) {
var i = 0,
plugins = this.plugins,
ln = plugins.length; //problematic
for (; i < ln; i++) {
if (plugins[i].pluginId === pluginId) {
return plugins[i];
}
}
},


My developer's thought is that the plugins.length check fails since plugins doesn't exist. Plugins isn't obligated to be defined.

The AbstractComponent.constructPlugins (http://docs.sencha.com/ext-js/4-1/source/AbstractComponent.html#Ext-AbstractComponent-method-constructPlugins) method isn't obligated to assign an array to plugins or return an array that would be assign to plugins from any calls while initializing the component. The configuration option (http://docs.sencha.com/ext-js/4-1/source/AbstractComponent.html#Ext-AbstractComponent-cfg-plugins) isn't defined as an array by default (wisely) either.

I'd say there's two options to repair:


getPlugins (and any other methods utilizing the plugins member variable) could guard against the case that the plugins array is undefined.
plugins could be initialized as an empty array by default, reducing the need to check


I'd guess the latter would be the better choice from a future proofing standpoint.

ojintoad
11 Feb 2013, 10:59 AM
Bug was eventually recognized as EXTJSIV-8568: http://www.sencha.com/forum/showthread.php?255996-getPlugin-fails-if-plugins-is-not-declared-as-an-array