How to use scope in store load function in ExtJS
Asked Answered
M

1

6
this.loadSmt = function(argPanel) 
{
    this.store1 = new Ext.data.JsonStore({
        url             : 'URL1',
        totalProperty   : 'total',
        successProperty : 'success',
        root            : 'root1',
        fields          : [
                            {name: 'data1'},
                            {name: 'data2'}
                        ]
    });

    this.store2 = new Ext.data.JsonStore({
        url             : 'URL2',
        totalProperty   : 'total',
        successProperty : 'success',
        root            : 'root2',
        fields          : [
                            {name: 'data21'},
                            {name: 'data22'},
                            {name: 'data23'},
                            {name: 'data24'},
                            {name: 'data25'}
                        ]
    });

    this.store1.on('beforeload', function() {
        this.store1.baseParams = {
            argID: myID
        };
    }, this);

    this.store2.load({
        callback    : function() {
            var graphOwner = argPanel;
            var graphDataArr = [];
            var i = 0;
            this.data.each(function(item, index, totalItems) {
                var data1 = item.data['data1'];
                    var data2 = item.data['data2'];

                    // create arraystore data for graph 
                    var tempArr = new Array();
                    tempArr.push(data1);
                    tempArr.push(data2);
                    graphDataArr[i] = tempArr;
                    i++;
            });


            this.GraphPanel = this.getMyGraph(graphDataArr);

        }
    }, this);
};

this.getMyGraph = function(argGraphValue)
{
    // do something
}

when I run this code I get

TypeError: this.getMyGraph is not a function

error, when I use

this.data.each

function in store callback, I can't use

this.getMyGraph

function. I think this is a scope problem, how can I pass scope as parameter to a function or how can I fix this error?

Mccloskey answered 19/11, 2012 at 11:52 Comment(0)
B
7

Your way to pass scope isnt right. You need to use scope parameter of loading options.

this.store2.load({
    callback    : function(records, operation, success) {
     ...
    },
    scope: this
});

http://docs.sencha.com/ext-js/4-1/#!/api/Ext.data.Store-method-load

Boyse answered 19/11, 2012 at 11:58 Comment(2)
I change my code as you show. Now I am getting TypeError: this.data is undefined error.Mccloskey
Change callback signature to callback: function(records, operation, success) and use records this.data insteadBoyse

© 2022 - 2024 — McMap. All rights reserved.