Singletons Ext JS (4.2.1) and Stores

3k views Asked by At

How do you implement a Store to be a singleton?

In Architect my store has a property checkbox that adds the property

singleton: true,

If I add that then reload the application it fails to load (I can give more details if required about error)

Uncaught TypeError: object is not a function VM5238:3
(anonymous function) VM5238:3
Ext.ClassManager.instantiate ext-all-debug.js:5485
(anonymous function) ext-all-debug.js:2109
Ext.define.getStore

If I then remove that property it loads fine, but where I use Ext.getStore("MyStore") then the stores that are returned contain different data depending on where I'm using the store. I have a single controller where I use the class name inside the getStore method and this is used in a couple of functions inside that one controller.

Also there does not appear to be any documentation about the singleton property in the docs. If I look at Ext.data.Store there is no singleton.

1

There are 1 answers

3
sra On BEST ANSWER

I recommend you to do it the ExtJS way for handling stores and store instances...

Use the storeId along with the Ext.StoreManager. Note that each storeId need to be unique and as soon as you create a instance of a store it will register itself into the Ext.StoreManager with its storeId. You can get a store by calling the lookup('storeId') on the Ext.StoreManager. And yes you can do that from anywhere in your code.

For example all Ext.Components that mixin Ext.util.Binable (which are most (all) native components that bind a store) will be fine with the storeId string assigned to the store cfg property. The Binable mixin will internally lookup it from the StoreManager. If you need to do it yourself

//...
store: Ext.StoreMgr.lookup('storeId') || Ext.create('YourStoreClassname')
//...