PDA

View Full Version : Best practice for loading a remote store only once



tm8747a
8 May 2013, 3:10 PM
I'm running into this often and wanted to confirm a good design pattern to deal with it. Basically, I have some remote stores that I know only need to be loaded once for the lifetime of the app, but since my application is quite large and there are a number of these stores, I'd like to load them only if needed, so basically if the user opens a view that requires it. What I've ended up using is the following utility function:



loadStore: function(storeId, store, cfg, callbackFn) {
if (!cfg) cfg = {};

var storeObj = Ext.StoreManager.lookup(storeId);
if (!storeObj) {
cfg.storeId = storeId;
storeObj = Ext.create(store, cfg);
storeObj.load({
callback: function() {
callbackFn();
}
});
} else {
callbackFn();
}
}


So basically with this I can wrap whatever code in my controller is going to load up a view that requires a store with this function. Inside the view I can use Ext.getStore('myStoreId') to get the store and the loadStore function will take care of making sure it's available. So the first time a store with that ID is requested through loadStore(), it will run an ajax request to get the data. Subsequently, it will simply invoke the callback function without running the Ajax request.

This is working fine for me, but just doesn't feel super clean. However, I haven't found any other good way to make sure a store is loaded short of pre-loading everything on application startup (I have a lot of these stores, would rather avoid that) or having a store with autload: true and set it in the controller's "store" config (seems this makes sure the store is loaded before anything in the controller runs as far as I can remember from my tests). I don't like the latter either, there are some controllers handling a number of different pages and it's entirely possible a user in a session would never need to hit a page loading a given store.

So bottom line, does my approach seem sound, or am I overengineering? I'm just worried about pre-loading a ton of data and running into memory issues.

slemmon
10 May 2013, 11:04 AM
Your code seems fairly condensed to me. And nothing comes to mind as to how to check for a store instance and create it if not created already better than what you're doing here.

I take it the stores are not very tightly coupled to components (or at least could stand alone)?

tm8747a
10 May 2013, 11:35 AM
Your code seems fairly condensed to me. And nothing comes to mind as to how to check for a store instance and create it if not created already better than what you're doing here.

I take it the stores are not very tightly coupled to components (or at least could stand alone)?

The stores are totally standalone and not coupled to components at all. Thanks for the feedback, guess I may be doing something right!

jay@moduscreate.com
10 May 2013, 11:39 AM
The stores are totally standalone and not coupled to components at all. Thanks for the feedback, guess I may be doing something right!

You're not taking scope into account for your callback, which may be dependent on execution scope.

tm8747a
10 May 2013, 11:48 AM
You're not taking scope into account for your callback, which may be dependent on execution scope.

Good catch. Hasn't really impacted me much thus far, but might as well make it better now. I'll make some changes to account for that, thanks for the suggestion.

jay@moduscreate.com
10 May 2013, 11:51 AM
this block could be better improved


storeObj.load({
callback: function() {
callbackFn();
}
});

to


storeObj.load({
scope : scope || storeObj,
callback: callback || Ext.emptyFn
});