Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Dynamically changing JsonStore root

  1. #1

    Question Dynamically changing JsonStore root

    Hi,

    fairly new to ExtJS. I have a similar query to this thread https://extjs.com/forum/showthread.php?t=31889 - but not quite so hence the new thread to avoid confusion. Apologies if it is more similar than I presume.

    I have a JsonStore which access Json data which has more than one array. I have a Form and a seperate gridPanel which I need to use essentially the same store but access different "root" elements within the Json data.

    The Json data is like this
    Code:
    { success: 'true',
       data:[ {id:1, firstName:'Bob, lastName:'Jones', phone:'0123456'} ],
       history:[ {histid:1, firstName:'Robert',lastName:'Jones', phone:'098765'},
                    {histid:2, firstName:'Bob',lastName:'Jones', phone:'098765'}]
    }
    And the JsonStore is defined thus:
    Code:
    var myStore = new Ext.data.JsonStore({
         proxy: new Ext.data.HttpProxy({  
            url: getData.php ,
                method: 'GET'
            }),
        fields: [{name:'id'},{name:'first_name'},{name:'last_name'},{name:'phone'}],
        id: 'id',
        root: 'data',
    )};
    I call the data into a form which loads the store and uses the data in the 'data' array. What I would also like to do is have the data in the 'history' array presented in a gridPanel. I'd sooner not make two calls to the underlying getData.php so is it possible to dynamically change the root config option of the store so that it uses the history array when called from the gridPanel?

    Can anyone point me in the right direction?

    TIA

  2. #2
    Sencha User Animal's Avatar
    Join Date
    Mar 2007
    Location
    Bédoin/Nottingham
    Posts
    30,892

    Default

    You will need to create two Stores.

    It might be more efficient to have two GridPanels in a card layout. One for each Store, and switch between each.

    But if you stick with one GridPanel, then use http://extjs.com/deploy/dev/docs/?cl...er=reconfigure to switch the Grid between the two Stores.

  3. #3
    Sencha User Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,246

    Default

    You can actually change the root of a JsonStore, but you need to use:

    Code:
    reader.meta.root = 'newRoot';
    delete reader.ef; // force metadata recompiling
    or
    Code:
    reader.getRoot = reader.getJsonAccessor('newRoot'); // modify compiled metadata

  4. #4

    Smile

    @Animal: thanks. This is actually two panels accessing one store (or intended to be) and only one of them is a grid but it looks like I may be better using two stores anyway.

    @Condor: another issue I've just spotted is that in my real situation the data and history arrays have slightly differing fieldnames (one additional field and one "renamed"). So it looks like I'll be better off having two stores anyway.

    thanks though

  5. #5

    Default

    Condor,
    Is the above solution version specific? Earlier i was using the solution with ext 3.0 and it was working fine and i was able to change the root dynamically. but now i have upgraded to ext 3.1 and not able to change the root now. It gives javascript error for the code.

    Please suggest.

    Thanks,
    Ruchika

  6. #6
    Sencha User Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,246

    Default

    Yes, in Ext 3.1+ you need to call buildExtractors() after clearing 'ef', because Ext won't do it automatically anymore.

  7. #7

    Default

    Is this how we need to write?
    Code:
    templateComboStore.reader.meta.root = 'newroot';
    delete templateComboStore.reader.ef;
    templateComboStore.buildExtractors();
    Thanks,
    Ruchika

  8. #8

    Default

    Thanks Condor,
    it worked.

  9. #9
    Sencha User vishalnnsingh's Avatar
    Join Date
    Sep 2010
    Location
    Kolkata, India
    Posts
    115

    Default

    Thanks Condor,

    You posted this long back, but helped me solve an issue today, do we still need to use the same code, or do we have a better solution for this?
    I Meet Challenge with Excitement & My Only Gossip is Love

  10. #10
    Ext JS Premium Member
    Join Date
    Mar 2011
    Posts
    178

    Default

    im also intrested to know for extjs4.0.7 please

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •