PDA

View Full Version : [SOLVED] ExtJs 4.2 Multiple instances of the same Store



marisradu
16 Apr 2014, 4:08 AM
Hi guys,

My situation is as follows, I'm trying to respect the DRY and reuse the code as much as possible (if it's still readable).
I have a grid that uses a store, and into another view I have a combo that need a read-only version of the data on the same store.

For example: Users/Groups, on the view to display the Users you have a combo to select a Group that should use the same Store as the view (grid) that displays the Groups. So far all filtering applied to the Groups store in the grid are visible in the combo to, because stores are singletons, is there an easy way to use a new instance of the same store ?

So far I tried to load it by hand and use something like this in the controller:

replaced
store: [App.store.Groups] to
Ext.create('App.store.Groups')

OR:

initComponent : function() {
var me = this;

Ext.apply(me, {
store : me.buildStore()
});

me.callParent(arguments);
},

buildStore : function() {
return Ext.create('App.store.Groups');
},


But all of my tests used the same store instance.

Zinnia
16 Apr 2014, 5:27 AM
Create new instance of store to each component i.e for grid and combo.
store: Ext.create('App.store.Groups');

If you want to use the filter to grid store only, then add this on grid render

listeners:{
render: function (grid){
grid.getStore().filterBy(function(record) { return record.get("name") == 'A';
});
}
}

scottmartin
16 Apr 2014, 6:11 AM
You could use the following:



Ext.define('App.store.MyStore', {
extend : 'Ext.data.Store',
alias : 'store.app-mystore', // create store alias

// ...
});

Ext.define('App.view.MyCombo', {
extend : 'Ext.form.field.ComboBox',
xtype : 'app-mycombo',

requires : [
'App.store.myStore'
],

// combo config

store : {
type : 'app-mystore' // store alias; type creates new instance
}
});

marisradu
22 Apr 2014, 1:01 AM
Sorry for the late answer, this solve the problem.
I had to set the store.autoLoad to true otherwise the combo will not trigger it's load() method.

Thank you.