0
votes

I have several stores in my application. They are not created until I load my grid which should call store.load() function. So some components may have a need of stores that are still not created. I am using this function to fix this problem:

getOrCreateStore: function(storeId, className){
    return (Ext.getStore(storeId) == undefined) ? Ext.create(className) : Ext.getStore(storeId);
},

So each time component has a need of specific store, I call if created or create it if it doesn't exist.

Is there a better way create the stores so that I don't have to manually do it? Are there some configurations I have missed to set up in my ExtJS 4 application regarding creation of stores?

Why I use the above fix is that grids stay empty if the store is not actually created first.

1

1 Answers

2
votes

See http://docs-origin.sencha.com/extjs/4.2.2/#!/api/Ext.data.StoreManager

  • You are not giving the store a storeId when creating it.
  • Your ternary can also be condensed into a || operation.

Code

getOrCreateStore: function(storeId, className){
    return Ext.getStore(storeId) || Ext.create(className, {storeId: storeId});
}

Side Note: I consider most Ext's managers to be bad practice. They are nothing more than glorified global variables. I prefer to pass around the stores where they're needed rather than resorting to adding them to global state. However, I do understand that this makes programming easier in some cases, and that if you want this lazy loading of stores (which I'm not sure will really benefit you much), this is the way to go. I'm just warning you that it tends to make maintenance and unit testing harder.