PDA

View Full Version : How are Stores really used by ExtJS?



fsleeman
12 Dec 2013, 1:56 PM
After of a lot of experimentation and online searching I am still at a loss of how stores are actually handed by ExtJS. What happens if I create one store which is used by two views:
1) the store hold the original and both views have “references” to the same data (like C++ pointer/reference)
2) the store holds the original data and each store has an independent copy of the original data
3) something totally different?


From the best I can tell, if I have one store with a filter both views will receive the same filtered results. If I want two different views using the same common data but filtered differently it appears that I have to create a different store for each view. Is that correct? It seems a waste to have to re-read the same data multiple time. Is this data getting read from a cache the second time? Is there some way to avoid data duplication or is there some smart functionality that takes care of this? I have not been able to find a complete explanation of how this works. Does is matter if you are using MVC or not?

tobiu
13 Dec 2013, 1:26 AM
You can bind a store to multiple views, but this will apply the same sorting, grouping and filtering.

In case you have a store with a server proxy (e.g. ajax), you can create a second store with a memory proxy and the same model. Listen to the load event of the first store and copy the data into the second store. This way you will not have to fetch the same data from your backend and can apply different filters.

fsleeman
13 Dec 2013, 6:33 AM
Thank you, that is very helpful. Do you know where I can find an example of how to copy the store data as you described? I don't think I have seen that online before.

scottmartin
13 Dec 2013, 3:54 PM
You can do something like this:



// 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); // true = append records to main
}
}
});

fsleeman
18 Dec 2013, 6:46 AM
I had a hard time trying to get this to work since I am using MVC. I cannot get if I should put that code in the controller or one of the stores. From the controller/store behavior I have seen it looks like I have to put it in the store because the load event is processed in the controller before the actual data is read. Am I correct that there is no post-loading event for stores?

tobiu
18 Dec 2013, 6:53 AM
not sure if i understand you correctly. the "post-loading" event is just load:
http://docs.sencha.com/extjs/4.2.2/#!/api/Ext.data.Store-event-load

fsleeman
18 Dec 2013, 7:47 AM
Sorry, I don't think I am being clear, let me rephrase my question.

Store A is loaded from a JSON file and I want to copy some of all of that data to another store B so I can avoid loading from disk a second time. I am assuming I need to wait for the store A to load and then copy the data to store B using the method shown in a previous post. I know how to access the stores from the controller but how do I access a store from another store?

gkohen
31 Jan 2014, 7:12 AM
This is almost like creating the tee command in Unix where you want the streams to two places. In this case you have a stream coming from the a server proxy and having some of the data copied over on load to the memory store. How about if I add remove or change model entries inside of the memory store and I want to propagate the changes to the server and or/other stores?
Should I listen on the controller in the "store" domain to these and have them manually propagate?