I am trying to use the 'ref' property on components for injecting back references like I could with Ext3. This was used on a lot of my previous code to get instances to various form fields that needed special handling. The nice thing was the ../ notation that allowed references to be placed where it was convenient.
Here is the method in Ext3's Ext.Component that handled it:
PHP Code:
initRef : function(){
/**
* @cfg {String} ref
* <p>A path specification, relative to the Component's <code>{@link #ownerCt}</code>
* specifying into which ancestor Container to place a named reference to this Component.</p>
* <p>The ancestor axis can be traversed by using '/' characters in the path.
* For example, to put a reference to a Toolbar Button into <i>the Panel which owns the Toolbar</i>:</p><pre><code>
var myGrid = new Ext.grid.EditorGridPanel({
title: 'My EditorGridPanel',
store: myStore,
colModel: myColModel,
tbar: [{
text: 'Save',
handler: saveChanges,
disabled: true,
ref: '../saveButton'
}],
listeners: {
afteredit: function() {
// The button reference is in the GridPanel
myGrid.saveButton.enable();
}
}
});
</code></pre>
* <p>In the code above, if the <code>ref</code> had been <code>'saveButton'</code>
* the reference would have been placed into the Toolbar. Each '/' in the <code>ref</code>
* moves up one level from the Component's <code>{@link #ownerCt}</code>.</p>
* <p>Also see the <code>{@link #added}</code> and <code>{@link #removed}</code> events.</p>
*/
if(this.ref && !this.refOwner){
var levels = this.ref.split('/'),
last = levels.length,
i = 0,
t = this;
while(t && i < last){
t = t.ownerCt;
++i;
}
if(t){
t[this.refName = levels[--i]] = this;
/**
* @type Ext.Container
* @property refOwner
* The ancestor Container into which the {@link #ref} reference was inserted if this Component
* is a child of a Container, and has been configured with a <code>ref</code>.
*/
this.refOwner = t;
}
}
},
removeRef : function() {
if (this.refOwner && this.refName) {
delete this.refOwner[this.refName];
delete this.refOwner;
}
},
Is this planned for Ext4 or was it intentionally removed?
If it has been removed, what is the best-practice alternative? There are times when validators and listeners aren't enough and manually instantiating is messy. Setting itemId on the sub-component and using Ext.getCmp(itemId) works but seems cumbersome and requires itemIds to be globally unique. For form fields, I can use Form.findField(name) but I was under the impression that it created extra overhead.