Search code examples
propertiesconfigextjs3

Extjs configoptions vs properties


A Java class has properties and methods for manipulating those properties. An ExtJS class has properties, methods and configOptions.
Conceptually, what is the difference between configOptions and properties? Why we need both?


Solution

  • As per my understanding…

    configs - are passed in the constructor, which defines behavior of the class, configs should not be changed at run-time because it will not have any effect, suppose you need to specify a title for the panel then you can add a config e.g. { title : 'some title' } that will be used by panel to set title of the panel at render time, but after that, even if you try to change title, you can't alter the property by simply changing that config option.

    properties - are used to store information which is useful for that class, this is normally not passed through constructor but should have getter and setter methods, you can change property at run-time (if setter method is defined) and class object should detect this change, there can be read only properties also which are modified by class object only we shouldn't change it all.

    More Info

    Sencha: Properties vs Configs, in the Ext 4 Documentation

    My answer to this question is a little simplistic and idealistic. I'm afraid trying to give a full answer that covers all the subtleties is more likely to add to the confusion rather than clarifying the situation.

    Config options are used to configure an object when it is created. Trying to set them as properties on the object after it has been instantiated will often have no effect.

    Ext.create('Ext.panel.Panel', {
        // config options go here
    });
    

    An object will have lots of properties but only the ones listed in the Properties section should be considered public properties. While there's nothing to stop you accessing the private properties you should only do it as a last resort, try to use the documented methods to manipulate them instead where possible.

    // rendered is a public property used to indicate whether the panel has been rendered
    if (panel.rendered) {
        // could just do panel.el but that isn't a public property, so use getEl instead
        var el = panel.getEl();
        ...
    }
    

    One reason why the lines get blurred is that objects generally copy their configs onto themselves like this:

    Ext.apply(this, config);
    

    This results in all the config options becoming private properties, at least initially. Internally classes can then manipulate those properties as appropriate but externally accessing those properties is a breach of encapsulation and should be avoided.