sencha-touchextjssencha-touch-2sencha-touch-theming

Registering a JSONm store in sencha touch


I am new in sencha touch application development .I am trying to load some data from remote we server to a list . I created a Store like the following

Ext.regStore('customers',{

model : 'customer',
sorters : 'firstName',
storeId : 'customers',
data : [{
    id : 100,
    firstName : 'aaa'
}, {
    id : 101,
    firstName : 'sss'
}, {
    id : 102,
    firstName : 'rrrr'
}]

});

Now I need to modify this store to retrieve data from external server.Follwoing is the code i am using right now

var customers = new Ext.data.JsonStore({
        model : 'customer',

           proxy : {
            type : 'ajax',
            url:'http:sample.com',

            reader : {
                type : 'json',
                root : '',
            },                              
        },

        listeners : {

            datachanged : function() {

                customers.each(function(r) {
                    console.log('data in record is:'+ r.get('name'));
                });
            }
        },
    });

Now my doubt is that how to register this JSON store like the inital code to access the store from another viewcontroller file.

Thanks in advance


Solution

  • You can use the property:

    storeId = "NameOfStoryID"

    After filling your store you can, later on in your code, use this storeId to access the store (get the data, update it etc).

    Example:

        var schouwLijstStore = new Ext.data.Store({
    
            model: "schouwLijst",
            storeId: "myStore",
            proxy: {
                type: 'ajax',
                url: 'php/json.php?t=list',
                reader: {
                    type: 'json',
                    root: 'list'
                },   
            },    
            autoLoad: true,
            listeners: {
                load: function() {
    
                                   // Do things on load (sync to offline store for example)
    
                }
    
            }
    
        });
    

    Now to access your store later on in the code you can use your storeID. For example to load it:

    // Load myStore.load();

    // Add item myStore.add({ id: *, value: * });

    etc.