PDA

View Full Version : How to copy the content of a store into another?



marxan
6 Jul 2012, 12:59 AM
Hello,

I have this issue for 2 days now and nothing seems to work.

What I want to do is very simple. I have 2 stores. I use the first one with a proxy to get data from DB.

Then I want to copy the datas I have retrieved to another store that I will link to a grid to display the data. I want to do this to avoid calling the database to much times.

Unfortunately I can't achieved that.

My first store


Ext.define('TAB.store.MetrSeriesStore', {
extend: 'Ext.data.Store',
requires: 'TAB.model.SerieGridModel',
model: 'TAB.model.SerieGridModel',
storeId: 'metrSeries',
groupField: 'CODEFC',
buffered: true,
pageSize: 100,
proxy: {
type: 'ajax',
url: 'app/data/loader.cfc?method=getMetrSeries&returnformat=json',
reader: {
type: 'json',
root: 'DATA'
}
}
});


The second store (should I also use a proxy to copy data in this one? )


Ext.define('TAB.store.MetrSeriesDisplayedStore', {
extend: 'Ext.data.Store',
requires: 'TAB.model.SerieGridModel',
model: 'TAB.model.SerieGridModel',
storeId: 'metrSeriestoDisplay',
proxy: {
type: 'rest',
reader: {
type: 'json',
root: 'data'
}
}
});




My controller that tries to copy data's



var store = Ext.getStore('MetrSeriesStore');


var storeForGrid = Ext.getStore('MetrSeriesDisplayedStore');


store.proxy.extraParams = {
codes : Global.codeListToLoad
}
// I load data in my first store
store.load();

// I get the values of the store
var records = [];
store.each(
function (r)
{
records.push (r.copy());
});
// I try to copy the values in my second store but it doesn't copy it
storeForGrid.loadRecords(records);


console.log(store);
console.log(storeForGrid);


Is there another way to do this action, it seems so simple to do it. But I wonder if extjs is able to do this kind of thing.

Any help would be greatly appreciated.

Regards

redraid
6 Jul 2012, 1:17 AM
storeForGrid.add(store.getRange());

//wbr

marxan
6 Jul 2012, 1:40 AM
storeForGrid.add(store.getRange());

//wbr

Thanks I already tried this (except that I was not adding the result but expecting return the good store :-s )
But now it only works if I link my first store to a grid, if I don't do that, my first store is not loaded. How comes?

sword-it
6 Jul 2012, 2:04 AM
Hi!

below is a sample code to copy content of a store into another (i have a grid and i am making a new store of records selected in grid.)




var selectedRecords = grid.getSelectionModel().getSelection();






Ext.define (http://docs.sencha.com/ext-js/4-0/#!/api/Ext-method-define)('User', {
extend: 'Ext.data.Model (http://docs.sencha.com/ext-js/4-0/#!/api/Ext.data.Model)',
fields: [
{name: 'firstName', type: 'string'},
{name: 'lastName', type: 'string'}
]
});

Ext.create (http://docs.sencha.com/ext-js/4-0/#!/api/Ext-method-create)('Ext.data.Store (http://docs.sencha.com/ext-js/4-0/#!/api/Ext.data.Store)', {
model: 'User',
data :selectedRecords ,
id: ...
..
});



you may modify your store as depend on your conditions and access it where ever you want

redraid
6 Jul 2012, 3:13 AM
Maybe first store not loaded when copy records to second - copy in load callback


store.load({
callback: function () {
storeForGrid.add(store.getRange());
}
});

scottmartin
6 Jul 2012, 9:59 AM
You should be able to use the following:




// main app store
var mainStore = new Ext.data.Store({
});

// loader store for new data; append records to main
var loadStore = new Ext.data.Store({
listeners: {
'load': function(store, records, successful) {
mainStore.loadData(records,true); // append records to main
}
});


Scott