Search code examples
javascriptextjsextjs4extjs-mvcextjs-stores

Initializing a store's data in Extjs


I have store that I would like to initialize from a database but I couldn't find a standard init method for the Ext.data.Store. I found a couple of examples with the StoreManager component, but I think that's not what I'm looking for. I have an MVC structure for my app and I'd like to keep it, I only want to initialize my store's data field using a method I define. Could someone explain how to do so?


Solution

  • I either understand you wrong or your question is straight forward. You configure a store with a model like this. That's all. You may just chose a provider(reader/writer) that fit your needs.

     // Set up a model to use in our Store
     Ext.define('User', {
         extend: 'Ext.data.Model',
         fields: [
             {name: 'firstName', type: 'string'},
             {name: 'lastName',  type: 'string'},
             {name: 'age',       type: 'int'},
             {name: 'eyeColor',  type: 'string'}
         ]
     });
    
     Ext.define('YourMVCNameSpace.data.UserStore', {
        extend: 'Ext.data.Store',
    
        constructor: function (config) {
             config = Ext.Object.merge({}, config);
             var me = this;
             // do what you need with the given config object (even deletes) before passing it to the parent contructor
             me.callParent([config]);
             // use me forth on cause the config object is now fully applied
         },
         model: 'User',
         proxy: {
             type: 'ajax',
             url: '/users.json',
             reader: {
                 type: 'json',
                 root: 'users'
             }
         },
         autoLoad: true
     });
    

    Note that the reader will expect a Json result like this:

    {"total": 55, "users":["...modeldata.."]}
    

    and is referring to a url like

    http://localhost/YourAppDomain//users.json
    

    Place the store as 'User' within the controller store array and retrieve it within the Controller by calling getUserStore() or directly from the Ext.StoreMgr using Ext.StoreMgr.lookup('User');

    Note that by convention the Controller (MVC) will override any storeId you set on the store and will just use the name.