Search code examples
javascriptextjs4

ExtJS 4 TreePanel autoload


I have an Ext.tree.Panel which is has a TreeStore. This tree is in a tab. The problem is that when my application loads all of the trees used in the application load their data, even though the store is on autoLoad: false.

How could I prevent autoloading on the tree?

Ext.define('...', {
    extend: 'Ext.container.Container',
    alias: 'widget.listcontainer',
    layout: {
        type: 'vbox',
        align: 'stretch'
    },
    items: [{
        xtype: 'container',
        html: "...",
        border: 0
    }, {
        xtype: '...',
        flex: 1,
        bodyPadding: 5,
        margin: '9 0 0 0'
    }]
});

Ext.define('...', {
    extend: 'Ext.data.TreeStore',
    model: '...',
    proxy: {
        type: 'ajax',
        reader: {
            type: 'json',
            root: 'data'
        },
        api: {
            read: 'some url'
        }
    }
});

Ext.define('...', {
    extend: 'Ext.tree.Panel',
    alias: 'widget....',
    id: '...',
    title: '...',

    height: 400,
    collapsible: true,
    useArrows: true,
    rootVisible: false,
    multiSelect: true,
    singleExpand: true,
    autoScroll: true,
    store: '...',

    columns: [...]
});

P.S. I've found out if I change rootVisible to true on the tree this problem doesn't happen, but then it shows to root node(which I don't want).


Solution

  • If root is invisible then AJAX tree will automatically load first level of hierarchy (as you already proved by yourself).

    I think the best way is to make root visible or create tree after some actions. I wrote code that prevent AJAX request that loads data:

    var preventTreeLoad = true;
    
    store.on('beforeexpand', function(node) {
        if (node == this.getRootNode() && preventTreeLoad) {
            Ext.Ajax.abort(this.proxy.activeRequest);
            delete this.proxy.activeRequest;
        }
    }, store);
    
    var b = Ext.create('Ext.Button', {
        text: 'Click me',
        renderTo: 'btn',
    });
    
    b.on('click', function() {
        preventTreeLoad = false;
        this.load();
    }, store);
    

    But I'm not recommended to use this approach. For example, if javascript wasn't so fast - Ajax request may be send (response will not be read but server will execute operation).