Skip to content

Instantly share code, notes, and snippets.

@vadimpopa
Last active July 4, 2016 14:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vadimpopa/7f39f01b7c35ec287e269b89c4a1835e to your computer and use it in GitHub Desktop.
Save vadimpopa/7f39f01b7c35ec287e269b89c4a1835e to your computer and use it in GitHub Desktop.
Sencha Ext.Config

In a previous post (http://moduscreate.com/a-dive-into-the-sencha-class-config-system/) Stan explained the foundation of Sencha Config System and how the getters and setters work. In this post I'm going to explain the new Ext.Config.configs which have been added in versions 5.0 and 5.5, which you can find here http://docs.sencha.com/extjs/6.0.2-classic/Ext.Config.html

  1. Ext.Config.cached

According to Sencha docs the cached config when set as true the config property will be stored on the class prototype once the first instance has had a chance to process the default value. To test that I've created a Fiddle: https://fiddle.sencha.com/#fiddle/1cpj.

```javascript
    config: {
        foo: {
            $value: 42,
            cached: true
        }
    }
```
[gif]

In ExtJS you can find an implementation in Ext.util.ElementContainer.childEls config. Along the cached config in you can see also the other configs which I've explained below, except the evented. ```javascript childEls: { $value: {}, cached: true, lazy: true,

        merge: function (newValue, oldValue, target, mixinClass) {
```

Checking for cached configs is easy by using the API of the Ext.Configurator class which is instantiated in the Ext.Base. Their reference is stored in the Ext.Configurator.cachedConfigs property (http://docs.sencha.com/extjs/6.0.2-classic/Ext.Configurator.html#property-cachedConfigs) which can be accessed trough the getConfigurator method:

myObj.getConfigurator().cachedConfigs
  1. Ext.Config.evented

This one if set as true the config property will be treated as a Ext.Evented which means whenever the setter of this config will be called ExtJS automatically will fire for you an event configname + change. Fiddle here: https://fiddle.sencha.com/#fiddle/1cno

Ext.define('MyClass', {
    mixins: ['Ext.mixin.Observable'],
    
    config: {
        foo: {
            $value: 42,
            evented: true
        }
    },
    
    constructor: function(config) {
        console.log('MyClass Instantiated');
        this.initConfig(config);
        this.mixins.observable.constructor.call(this, config);
        return this;
    }
});

myObj = new MyClass();

myObj.on('foochange', function() {
   console.log(arguments);
});

[gif]

  1. Ext.Config.lazy

Any lazy config if set as true the config property will not be immediately initialized during the initConfig call. Fiddle here https://fiddle.sencha.com/#fiddle/1c52, and the declaration is:

    config: {
        foo: {
            $value: 42,
            lazy: true
        }
    }

[gif]

  1. Ext.Config.merge

Merge config accepts a function which will be called as instances provide values that need to be combined with inherited values. The returned value of the function will be the config value and on further calls that returned value will be provide as oldValue trough an argument

Ext.define('MyClass', {
    config: {
        foo: [42]
    },
    constructor: function(config) {
        this.initConfig(config);
        return this;
    }
});

Ext.define('MyExtendedClass', {
    extend: 'MyClass',
    
    config: {
        foo: {
            $value: [23],
            merge: function(newValue, oldValue) {
                var val = [].concat(newValue, oldValue);
                
                debugger;
                return val;
            }
        }
    }
});

myObj = new MyClass();
myExtendedObj = new MyExtendedClass();

https://fiddle.sencha.com/#fiddle/1cpo

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