5

I would like to have different instances of the same view with different stores at the same time in an ExtJS application. At the moment i ceate multiple instances of the same view (Ext.view.View) in the viewport.

But what is the best practice to have a different store in every view? Every example that i found uses a Store-ID in the view that was created using the stores-Config of the controller. But this would use the same store for every view.

At the moment i figured the following possible solutions:

  1. Create an own store class for every view instance. Add all stores to controller and use different Store-ID for every view instance.
  2. Do not use stores of controller at all and create a new store in the initComponent of the view manually passing different parameters to each instance of the store.
  3. Do not use stores of controller at all and create a new store in the initComponent of the view manually. Then use load to load the store manually using different parameters for each instance of the store.

Is any of this solutions the best practice or should it be done differently?

code4jhon
  • 4,785
  • 8
  • 36
  • 54
Werzi2001
  • 1,745
  • 1
  • 13
  • 32
  • 2
    Downvote without comment is so useless... – Werzi2001 Dec 06 '14 at 16:49
  • It's a fair question, I don't see why this was downvoted. – code4jhon Dec 06 '14 at 17:31
  • Thanks. Btw i decided for solution 3 for the time until a better solution was posted. – Werzi2001 Dec 06 '14 at 17:37
  • One of the problems I can think is the extra code to destroy those stores when you destroy the associated component. I mean if you create the store in the initComponent function this store is probably ghoing to stay around when you destroy the associated grid ... – code4jhon Dec 06 '14 at 17:37
  • Shouldn't the garbage collector take care of that? If i destroy the view that holds the reference to the store the store wouldn't be referenced anymore? I think it would be too much overhead to have multiple view and store classes just for having multiple views with different store parameters, isn't it? – Werzi2001 Dec 07 '14 at 15:58
  • I absolutely agree about defining multiple views /stores for this is not an option, and yes I guess the garbage collector will get rid of this store as there is anything referencing to it. – code4jhon Dec 08 '14 at 23:14
  • Already asked here http://stackoverflow.com/questions/27368951/are-stores-created-in-the-initcomponent-function-memory-leaks-once-the-component – code4jhon Dec 08 '14 at 23:31
  • Thanks for the additional question. Will have a look at the answers. – Werzi2001 Dec 09 '14 at 08:23

1 Answers1

3

The thing with the stores array of the controller is, that it will override any storeId defined. After loading the store class the controller set the storeId by a namespace convention, create the store and create the getter method method by using the value as the soreId.

Let me introduce option 4

  • Define one store for the view and require it in the view (you can also require it within the controller, just use the requires array).
  • Choose valid itemId's for the views and valid storeId's for your store that should depend on the itemId of the view (set it when creating the view!).
  • Within the initComponent create the storeId and lookup the store in the StoreManager. If it not exist create it and supply a Customer config and the storeId.

If you need to destroy the view and the store each time take a look at this post

Demo initComponent

initComponent: function() {
    var me = this,
        storeId = me.storeId + me.itemId;
    me.store = Ext.StoreManager.lookup(storeId);
    if(me.store === null)
        me.store = Ext.create('App.data.CustomViewStore',Ext.apply({storeId: storeId},me.storeCfg || {}));
    me.callParent(arguments);
}

Note: If you load the view with the views array you will end up with a getter that may not give you the view you expected. You may use the requires array of the controller here, too. If you want to use getter simply create your own one by using the refs config.

Community
  • 1
  • 1
sra
  • 23,629
  • 7
  • 52
  • 88
  • Thanks for your reply but i can't see the "big" difference to my option 2. Is it just about the storeId and Ext.StoreManager or am i missing something? – Werzi2001 Dec 17 '14 at 14:40
  • @Werzi2001 No, it is not a big difference. But it is a difference which make sense! The thing is that you can still define your store at is own and don't pollute your views `initComponent` method with it. – sra Dec 17 '14 at 15:15