Search code examples
javascriptextjsextjs4

Change store load from view to controller ExtJS4


I have the next code. My View:

Ext.define('Test.view.mantenimientos.organismos.viewGridMtoOrganismos', {
extend: 'Ext.grid.Panel',
alias: 'widget.viewGridMtoOrganismos',
requires: [
],
initComponent: function() {
    var toolbar1 = {
            xtype : 'toolbar',
            dock : 'top',
            items: [
                    {
                        iconCls:'limpiar-icon', text:'Limpiar', handler:  function()    {},
                    },
                    '->',
                    {
                        iconCls:'refresh', text:'Recargar', handler:  function()    {},
                    }
            ]
    };

    var toolbar2 = {
            xtype: 'toolbar',
            dock: 'top',
            items: [
                    {text:'<span style="color:#C85E00;">Estado</span>'},
                    {
                        xtype: 'combo',
                        id: 'comboEstados',
                        value: 'Todos',
                        queryMode: 'local',
                        editable: false,
                        displayField: 'label',
                        valueField: 'value',
                        store: 'filtros.strEstadosMtoOrganismos'
                    },
                    {text:'CCAA'},
                    {
                        xtype: 'combo',
                        id: 'comboCCAA',
                        value: 'Todas',
                        queryMode: 'local',
                        editable: false,
                        displayField: 'label',
                        valueField: 'value',
                        store: 'filtros.strComunidadesAutonomas',
                        matchFieldWidth: false
                    },
            ]
    }   

    Ext.apply(this, {
        frame: true,
        bodyPadding: '5 5 0',
        fieldDefaults: {
            labelAlign: 'top',
            msgTarget: 'side'
        },
        forceFit: true,
        height: 300,
        stripeRows: true,
        loadMask: true,
        tbar: {
            xtype: 'container',
            layout: 'anchor',
            defaults: {anchor: '0'},
            defaultType: 'toolbar',
            items: [
                    toolbar1,toolbar2
            ]           
        },
        columns: [
                  {header:'<span style="color:blue;">Id</span>', xtype: 'numbercolumn',format:'0',  width:35, sortable: true},
        ]
    });
    this.callParent(arguments);
    Ext.getCmp('comboEstados').getStore().load();
}
});

My controller:

Ext.define('Test.controller.ctrlMtoOrganismos', {
extend: 'Ext.app.Controller',
models:[
        'mantenimientos.organismos.mdlMtoOrganismos',
        'filtros.mdlEstadosMtoOrganismos'

],
stores:[
        'mantenimientos.organismos.strMtoOrganismos',
        'filtros.strEstadosMtoOrganismos',
        'filtros.strComunidadesAutonomas'
],
views: [ 
        'mantenimientos.organismos.viewModuloMtoOrganismos'
],
refs: [
],
init: function() {

}
});

Actually I load the store in the view,but I think it´s better to load it in the controller,MVC. But I don´t want the store to load on the application launch,I want to load the store(Ext.getCmp('comboEstados').getStore().load();)from the controller when the view is loaded.


Solution

  • I'm more familiar with ExtJS >= 5.0 but i think that the following code could solve your problem.

    In your controller:

    init: function() {
        this.control({
            'viewGridMtoOrganismos': {
                 beforerender: this.loadStore
             }   
        })
    },
    
    this.loadStore: function(view){
        Ext.getCmp('comboEstados').getStore().load();
    }
    

    In this case, i'm using beforerender (to load store when the view is completely rendered) but you can use afterrender or render.

    As a tip, you should check some of the Sencha info about nomenclature (in terms of widget names) and good practices while coding.