Search code examples
extjsextjs4extjs4.2

Extjs - Retrieve xtype using custom config


I have defined an xtype:

Ext.define('Sample.view.CustomTextfield', {
  extend: 'Ext.field.Text',
  customConfig: 'foo'
  . . . .
});

I want to now if there is a method to create a component using the customConfig.

doing something like myPanel.add(Ext.getComponentByConfig('customConfig', 'foo'));

Is there an extjs to do so?

NOTE:

The custom component is not yet rendered but is already defined so answers using ComponentQuery and the like (.up(), .down(), etc..) is incorrect.

EDIT:

Here's a sample use-case:

Supposedly you have an array of object key-value pairs:

var myArray = [
  { config: 'customConfig', value: 'foo' },
  { config: 'anotherCustomConfig', value: 'bar' },
  . . . .
]

Now, what I need to do is to loop over the array and retrieve the config property which I will then use to retrieve the xtype that has that config with a value equal to value, i.e, for config customConfig with a value foo. In short, I want to dynamically render views using an array of config->value objects.


Solution

  • So, here's whay I came up with. I don't know if this is the right way to do it but I will share this anyway. Please feel free to improve this:

    Ext.createBy = function (config, value) {
        var classes = Ext.ClassManager.classes;
        for (var cls in classes) {
            if (classes.hasOwnProperty(cls)) {
                var c = classes[cls];
                if ((c[config] && c[config] === value) || (c.prototype && c.prototype[config] && c.prototype[config] === value)) {
                    return Ext.create(classes[cls].getName());
                }
            }
        }
    }
    

    What this function, Ext.createBy does is iterate over the classes which are held by Ext.ClassManager and check if the class has a config and value supplied in the parameters. Then it calls Ext.create using the which passed the tests.

    This can be used like this:

    myPanel.add(Ext.createBy('someConfig', 'someValue'));

    or, pertaining my sample use-case:

    var myArray = [
      { config: 'customConfig', value: 'foo' },
      { config: 'anotherCustomConfig', value: 'bar' },
      . . . .
    ];
    
    for (var i = 0, l = myArray.length; i < l; i++) {
       var obj = myArray[i];
       myPanel.add(Ext.createBy(obj.config, obj.value);
    }
    

    This will dynamically add items to myPanel depending on the config->value.