I have three stores in my application EmployeesTree, Empolyees, History. First one is source for treegrid, second for combobox that is used to pass additional parameters while loading third stode.
My first approach was to create second store using standard proxy and load employees from server using request.
In my history grid I have:
me.c = Ext.create("MyApp.Store.Employees");
me.c.load();
me.tbar=
[
{
xtype: 'combo',
fieldLabel: 'Employee',
width: 300,
labelWidth: 65,
typeAhead: false,
forceSelection: true,
store: me.c,
queryMode: 'local',
displayField: 'Name',
valueField: 'Id',
listeners: {
select: function (combo, records) {
console.log(records[0].get('Id'));
me.store.proxy.extraParams.uid = records[0].get('Id');
me.store.load();
},
scope: this
}
}
];
But this cause additional request that I would like to avoid.
My second idea was to create empty store and add every leaf from first store to it (because it will contains the same list of employees but without hierarchy)
I've created method that should copy all leafs to new store:
cloneStore: function(source) {
var target = Ext.create('Sch.data.ResourceStore', {
model: 'MyApp.Model.Employee'
});
var node = source.getRootNode();
node.eachChild(function (myNode) {
if (myNode.isLeaf()) {
var newData = Ext.clone(myNode.copy().data);
var model = new source.model(newData, newData.id);
target.add(model);
}
});
Right now this copies first level of my store, I need to add recursive to this method so it will add leafs from subnodes.
Also when I do var newData = Ext.clone(myNode.copy().data);
my new record will have all fields of that treestore, how can I copy only two fields from my source store to target. I need only 2 fields (id, Name).
How should I modify cloneStore
method to recursively copy employees from treeStore to Store and how can I copy only needed fields? Do I must specify them in my method.
EDIT I've modified my function so it can be called recursively.
cloneStore: function(source) {
var target = Ext.create('MyApp.Store.EmployeesForComboBox');
var node = source.getRootNode();
this._addLeafsToStore(target, node);
return target;
},
_addLeafsToStore: function(store, node) {
node.eachChild(function (myNode) {
if (myNode.isLeaf()) {
store.add({
Id: myNode.get('Id'),
Name: myNode.get('Name')
});
} else {
this._addLeafsToStore(store, myNode);
}
},this);
},
This works quite well, but I would like to create more universal function, because right now I have hardcoded Store and fields I want to copy.
Additional question:
What option is better: to add new record to store using Ext.clone (first solution) or adding only necessary fields (second solution)?