Search code examples
extjsextjs4

load store with dynamic param


I want to load a record based on my tree node selection and show it in my form. But my store is not loaded in my controller handler. Where and how should I load the store with my parameter?

        var me = this;
        var idTag= me.getMyNode();
        me.getTagStore().on('beforeload', function(store, operation, eOpts) {
            operation.params = {
                idTag: idTag
            };
        }, me);
        // it will not be loaded here and I get rec=undefined
        var rec = me.getTagStore().load().getAt(0);
        me.getMyForm().loadRecord(rec);

And here is my store:

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

    proxy: {
        type: 'ajax',
        url: 'tag/find.json',
        reader: {
            type: 'json',
            root: 'data'
        }
    }
    ,baseParams: {
        idTag: 30
    },
});

Solution

  • You can call the load method manually whenever needed and pass any parameters needed, like this:

    me.getTagStore().load({
        params: {
            idTag: idTag
        }
    });
    

    When you call this you should see your request with the parameter in your console window in the browser.

    In your code above, this line:

    // it will not be loaded here and I get rec=undefined
    var rec = me.getTagStore().load().getAt(0);
    

    The load operation takes a little time, not much but it does take a little time. You need to listen to the load event on your store to be able to do things like this, here is an example:

    me.getTagStore().load({
        params: {
            idTag: idTag
        },
        scope: this,
        callback: function(records, operation, success) {
            // the operation object
            // contains all of the details of the load operation
            console.log(records);
        }
    });
    

    Checkout the documentation for store.