0

Possible Duplicate: How to set url and root dynamically in extjs

Hi there, I have a simple memoryStore. If I tried not to declare its proxy during the Ext.Define, I am unable to retrieve the proper data root later on, even if I do set the proxy. Am I doing something wrong?

Here's a test case:

var store = Ext.create('Ext.data.Store', {
    storeId: 'JailNames',
    autoLoad: true,
    fields: [
        {
            name: 'name',
            type: 'string'
        },
    ],
    data: {
        data_regionI: [
                    {name: "Jail 1"},
                    {name: "Jail 2"},
                    {name: "Jail 3"},
        ],
        data_regionII: [
                    {name: "Jail 4"},
                    {name: "Jail 5"},
                    {name: "Jail 6"},
        ],
    },
    proxy: {
        type: 'memory',
        reader: {
            type: 'json',
            root: 'data_regionI'
        }
    }
})



  store.setProxy({
          type: 'memory',
          reader: {
              type: 'json',
              root: 'data_regionII'
          }
      } )

store.load();
store.getAt(0).raw //still returns Jail 1

Looking through store.getProxy().reader.root I get the data_regionII as a root. Why? Thanks in advance

Community
  • 1
  • 1
niCad
  • 82
  • 1
  • 12

1 Answers1

1

If I copy your code into a sencha fiddle of Version 4.1.1, it throws an Uncaught TypeError: Cannot read property 'raw' of undefined, which is what I expected, because the store shouldn't contain any records at all after the call to load().

There are many problems in your understanding what a store does and what a proxy does:

  • A normal store's load function will tell the proxy to fetch the data, tell the reader to make records from it, and load it into the data property of your store, overwriting(!) the data you have defined at initialization.
  • But a memory store's load function isn't intended to do anything at all, and isn't intended to be used at all.
  • A memory store isn't intended to hold more than one store content at the same time. (you can, however, store the unused contents in an unused(!) property of the store's JavaScript object).
  • A store, no matter which proxy, does not require autoLoad:true to load the content of data into the store - the content of data is automatically used as the default data of the store after initialization.

That said, it's still possible to achieve what you want with just a few lines of code. You don't even have to create all the functions I only made for readability:

var store = Ext.create('Ext.data.Store', {
    storeId: 'JailNames',
    fields: [
        {
            name: 'name',
            type: 'string'
        },
    ],
    myData: { // custom property!
        data_regionI: [
                    {name: "Jail 1"},
                    {name: "Jail 2"},
                    {name: "Jail 3"},
        ],
        data_regionII: [
                    {name: "Jail 4"},
                    {name: "Jail 5"},
                    {name: "Jail 6"},
        ],
    },
    loadRegion1:function() {
        this.loadRegion("data_regionI");
    },
    loadRegion2:function() {
        this.loadRegion("data_regionII");
    },
    loadRegion:function(rootProperty) { // custom function for better readability
        this.loadRawData(this.myData[rootProperty]); // load data without proxy, but with reader!
    },
    proxy: {
        type: 'memory',
        reader: {
            type: 'json'
        }
    }
});
store.loadRegion1();
console.log(store.getAt(0).get("name")); //returns Jail 1
store.loadRegion2();
console.log(store.getAt(0).get("name")); //returns Jail 4
Alexander
  • 19,906
  • 19
  • 75
  • 162
  • I see, thanks for the clarification, Alexander! I understand what you meant by this. Ups for you! – niCad Jul 05 '16 at 00:54