extjs - Store with autoload true should not load on application launch

后端 未结 3 1079
误落风尘
误落风尘 2021-02-15 16:54

I have a grid linked to a store with autoLoad: true. The problem is that the store gets loaded on application launch, even if the view is created only later when ac

相关标签:
3条回答
  • 2021-02-15 17:29

    This is my final controller code:

    Ext.define('Mb.controller.Winbiz', {
        extend: 'Ext.app.Controller',
        views: [
            'Owners'
        ],
        refs: [{ref: 'testGrid', selector: 'test-gridPanel'}],
        init: function(){
            this.listen({
                store: {
                    '#Owners':{ load: this.onOwnersLoad}
                }
            })
            this.control({
                'menu #test': {click: this.onMenuTest},
                'test-gridPanel': {render: this.onOwnersRender}
            })
        },
        onMenuTest: function(){
            this.getController('Main').addToMainTab('test-gridPanel');
        },
        onOwnersLoad: function(store){
            store.loaded = true
        },
        onOwnersRender: function(){
            var store = this.getTestGrid().getStore();
            if(!store.loaded)store.load();
        },
    

    It puts all code into the controller as suggested by @pcguru and uses the render event to shorten the code as suggested by @Lolo. Thanks

    0 讨论(0)
  • 2021-02-15 17:34

    I would let the view's controller handle the store load.

    Start by disabling autoload on the store.

    Ext.define('Mb.controller.Winbiz', {
        extend: 'Ext.app.Controller',
        views: [
            'Owners'
        ],
        ownerStore: null,
        init: function(){
            this.control({
                'menu #test': {click: this.onMenuTest},
            });
    
            this.ownerStore = Ext.getStore('winbiz.Owners');
        },
        onMenuTest: function() {
            if (this.ownerStore.loaded === false) {
                this.ownerStore.load(
                    scope: this,
                    callback: this.onOwnerStoreLoaded
                );
            }
            else {
                this.addToTab();
            }            
        },
        onOwnerStoreLoaded: function (store, records, successful, eOpts) {
            if (successful) {
                store.loaded = true;
                this.addToTab();
            }
        },
        addToTab: function () {
            this.getController('Main').addToMainTab('test-gridPanel');
        }
    

    Wheter you want to change the view before or after the store is loaded is another question.

    0 讨论(0)
  • 2021-02-15 17:38

    You can add render handler to view which will call store load method and disable autoLoad.

    Example listener:

    Ext.define('Mb.view.winbiz.Owners', {
        extend: 'Ext.grid.Panel',
        [...],
    
        initComponent: function(){
            this.callParent();
            this.on('render', this.loadStore, this);
        },
    
        loadStore: function() {
            this.getStore().load();
        }
    });
    
    0 讨论(0)
提交回复
热议问题