Skip to content

Instantly share code, notes, and snippets.

@wingspan
Created May 25, 2012 14:36
Show Gist options
  • Select an option

  • Save wingspan/2788469 to your computer and use it in GitHub Desktop.

Select an option

Save wingspan/2788469 to your computer and use it in GitHub Desktop.
ExtJS ComboBox auto-configuration
Ext.namespace('wspt.form');
/**
* This plugin enables a combobox (or subtype) to configure its "valueField" and "displayField"
* from the metadata defined in a JsonStore.
*/
wspt.form.ComboConfigPlugin = Ext.extend(Object, {
init : function (combo) {
function setComboFields (store, meta) {
// ID field maps to combo value
this.valueField = meta.idProperty;
// for lack of better choice, use the first field as the display
this.displayField = store.fields.first().name;
// the template might have been created when "displayField" was undefined. Fix that.
if (this.tpl) {
this.tpl = this.tpl.replace("undefined", this.displayField);
this.view.tpl = new Ext.XTemplate(this.tpl)
}
}
combo.store.on('metachange', setComboFields, combo, {single: true});
}
});
Ext.preg('wsptComboConfigPlugin', wspt.form.ComboConfigPlugin);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment