First, I will focus on my comments:
@ Alexander Tokarev Do not get me wrong. I'm not talking about component configurations or, even worse, about cases, and porting them to initComponent() , this is not my point.
Now I think about it.
initComponent () should allow all necessary time for an instance of this class. No more no less.
You can spoil the load when defining classes, and most of them happen because people don’t understand how the ExtJS class system works. Since these are components, the focus will be on these. This will also be a simplified example, which should only show some error that I have seen many times.
Let's get started: we have a custom panel that does a lot of great stuff. This necessitates a custom configuration, we call it foo . We add it along with our default configuration option to define the class so that we can access it:
Ext.define('Custom', { extend: 'Ext.panel.Panel', alias: 'widget.custpanel', foo: { bar: null }, initComponent: function() { this.callParent(arguments); } });
But after testing everything becomes strange. The values ​​of our configurations seem to magically change. Here JSFiddle It so happens that all created instances refer to the same instance of foo . But lately I made it
store: { fields: [ ... ], proxy: { type: 'direct', directFn: 'Direct.Store.getData' } }
with the store, and it worked. So why doesn't foo work?
Most people see no difference between this small foo object and the configuration (ExtJS), which is basically correct because both are objects (instances). But the difference is that all classes sent by sencha are well aware of what configuration properties they expect and take care of them.
For example, the grid storage property is enabled by StoreManager and therefore may be:
- Line
storeId or- store an instance or
- save configuration object.
During grid initialization, any of them gets permission and is overwritten by the actual storage instance. A store is just one example. I think the more famous is the array of elements. This is an array at definition time and it is redefined for each instance using a MixedCollection (if I'm not mistaken).
Yes, there is a difference between a class definition and the instance it creates. But we need to take care of any new property that contains a link like foo top, and it's not that difficult. Here is what we need to do to fix it for foo example
Ext.define('Custom', { extend: 'Ext.panel.Panel', alias: 'widget.custpanel', foo: { bar: null }, initComponent: function() { this.foo = Ext.apply({}, this.foo); this.callParent(arguments); } });
Here is jsfiddle
Now we take care of config foo when creating the instance. Now this foo example is simplified and it will not always be easy to resolve the configuration.
Conclusion
Always write class definitions as configurations! They should not contain any of the instances mentioned, except for a simple configuration, and should take care of this to allow them to be instantiated.
Renouncement
I do not pretend to cover all this very short letter!