Diêgo Nunes
Diêgo Nunes

Reputation: 19

Error Store Load

Good evening.

The initial view of my application need to get some data from a Store (autoLoad = true) to change some components of it. At what point can I do this?

View:

    Ext.define('App.view.EmpresaPanel', {
    extend: 'Ext.Container',
    alias: 'widget.empresapanel',

    config: {
        ui: 'dark',
        layout: {
            type: 'card'
        },
        items: [
            {
                xtype: 'titlebar',
                docked: 'top',
                title: 'Menu',
                layout: {
                    align: 'center',
                    type: 'hbox'
                }
            },
            {
                xtype: 'titlebar',
                docked: 'bottom',
                title: '2012 ©',
                layout: {
                    align: 'center',
                    type: 'hbox'
                }
            }       
        ]
    }

});

Store:

Ext.define('App.store.Empresa', {
    extend: 'Ext.data.Store',
    requires: [
        'App.model.Empresa'
    ],

    config: {
        autoLoad: true,
        autoSync: true,
        model: 'App.model.Empresa',
        storeId: 'EmpresaStore',
        proxy: {
            type: 'ajax',
            url: 'http://URL',
            reader: {
                type: 'json',
                rootProperty: 'empresa'
            }
        }
    }
});

Controller:

Ext.define('App.controller.Empresa', {
    extend: 'Ext.app.Controller',
    config: {
        refs: {
            empresaPanel: 'empresapanel'
        },

        control: {
            "empresapanel": {
                initialize: 'onContainerInitialize'
            }
        }
    },

    onContainerInitialize: function(component, options) {
        var store = Ext.getStore("EmpresaStore"),
        record = store.findRecord("id", "1");

        console.log(store);
        console.log(record);

    }

});

First appears in console.log: Ext.apply.create.Class In the second: null

If I put a button in this view and tap the same event I run the same code that is in the function "onContainerInitialize" the record is completed.

Is there a specific time after the creation of view where you can access data from the Store?

Thank you!

Upvotes: 0

Views: 724

Answers (1)

Volodymyr
Volodymyr

Reputation: 181

You should add listener for store load event to access record from the store. Here is an example how you can do it. Put following code inside of the onContainerInitialize:

store.on('load', function(t) {
    var record = t.findRecord("id", "1");
    console.log(record);
}, this, {single: true});

Upvotes: 1

Related Questions