Results 1 to 5 of 5

Thread: ViewController stores are shared across instances

  1. #1
    Sencha Premium User
    Join Date
    Sep 2011
    Location
    Tamworth, NSW, Australia
    Posts
    1,353
    Answers
    13

    Question ViewController stores are shared across instances

    How do you get a store in a ViewController to be unique if you create multiple instances of the associated view? Here's a fiddle demonstrating the problem (using code created with Sencha Architect 3.5.1).

    If you link a form in a tab panel or create it on the fly, the store is still shared across instances. Changes to cells in one grid are reflected in the other tabs when you refresh their respective grids :-(

  2. #2
    Sencha Premium User evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    19,250
    Answers
    758

    Default

    The stores aren't shared, in fact it's not related to MVC at all.

    The issue is that you're using the memory proxy as the "server". By default, it doesn't clone the underlying object data, so as you make changes to the record in store 1, it's modifying the underlying data object in the other record in the other store.

    You can force it to not do this by setting preserveRawData: true

    Code:
    var storeCfg = {
        data: [{
            id: 1,
            name: 'Foo'
        }],
        proxy: {
            type: 'memory',
            reader: {
                type: 'json',
                preserveRawData: false // toggle me
            }
        }
    };
    
    var s1 = new Ext.data.Store(storeCfg);
    var s2 = new Ext.data.Store(storeCfg);
    
    console.log(s1 === s2);
    console.log(s1.first() === s2.first());
    console.log(s1.first().data === s2.first().data);
    This is typically not a problem because the data comes from a real server where the object references aren't the same.
    Twitter - @evantrimboli
    Former Sencha framework engineer, available for consulting.
    As of 2017-09-22 I am not employed by Sencha, all subsequent posts are my own and do not represent Sencha in any way.

  3. #3
    Sencha Premium User
    Join Date
    Sep 2011
    Location
    Tamworth, NSW, Australia
    Posts
    1,353
    Answers
    13

    Default

    I only used a memory proxy so that I could create a fiddle which works without my web services. When I use Ajax or Rest proxies, I have the same problem.

    I checked the (now new!) docs and couldn't see a preserveRawData setting for an Ajax Proxy (perhaps it's there and I'm still figuring out this new doc layout)?

    Edit: oops, it's in JsonReader. So this should fix the problem or it's only related to memory proxies?

  4. #4
    Sencha Premium User mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    40,449
    Answers
    3997

    Default

    Fiddle doesn't have a good dynamic data mechanism yet so the data will look the same. So for an easy way to view the stores, I modified the MyFormViewModel.js to have a store definition so that in the constructor I could create a property and use Ext.id() to have a unique identifier:



    So after clicking on the tabs, if I run this in the console:

    Code:
    Ext.ComponentQuery.query('gridpanel').forEach(function(grid) {
        console.log(grid.getStore().foo);
    });
    I can see that the constructor I have in the MyStore definition was called three times as each store has a unique foo property.

    You can also run this test to check if the stores match each other (so long as you clicked on all tabs to create the grids):

    Code:
    var grids = Ext.ComponentQuery.query('gridpanel');
    
    if (grids[0].getStore() === grids[1].getStore() || grids[1].getStore() === grids[2].getStore()) {
        console.log('store instances are shared!');
    } else {
        console.log('none of the stores are being shared');
    }
    And I get the 'none of the stores are being shared' logged.

    I hope this helps prove the point that store instances are not shared.

    Just for giggles, I even switched out the memory proxy for the ajax proxy (will show same data set as the data1.json is a static data set):

    Mitchell Simoens @LikelyMitch
    Modus Create, Senior Fullstack Engineer
    ________________
    Modus Create is based on the model of an open source team. We’re a remote, global team of experts in our field. To find out more about the work we do, head over to our website.

    Check out my GitHub:
    https://github.com/mitchellsimoens

  5. #5
    Sencha Premium User
    Join Date
    Sep 2011
    Location
    Tamworth, NSW, Australia
    Posts
    1,353
    Answers
    13

    Default

    I started modifying my stand-alone example yesterday to work with a real web service and couldn't quite reproduce the problem, but I didn't pick the exact setup that lead me to believe it had problems to begin with. I need to spend more time on it.

    I was also experiencing "cross contamination" from viewmodel.links and formpanels so perhaps that was more my problem? I abandoned the cloning of forms and moved on so I'll spend more time on a test case and post again.

    Thanks (and sorry for wasting your time for now).

Similar Threads

  1. Sync Lifetime instances with ViewController
    By oorduz.ps in forum Ext 5: Q&A
    Replies: 4
    Last Post: 31 Oct 2014, 9:55 AM
  2. Toolbar being shared across mulitple instances of view
    By jascolley in forum Sencha Touch 1.x: Q&A
    Replies: 4
    Last Post: 1 Mar 2012, 11:14 AM

Tags for this Thread

Posting Permissions

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