在extjs中将局部变量设置为全局变量

时间:2018-02-13 08:10:13

标签: javascript extjs global-variables

如何将局部变量设为全局?

Ext.define('A.view.Management', {
extend: 'Ext.container.Container',

requires: [
    'A.store.Management',
],

border: false,
chart: null,

hrer: [],   

initComponent: function () {
    var me = this;

    me.jobStore2 = Ext.create('O2a.store.Management');


    Ext.Ajax.request({
        url: utils.createUrl('api', 'ard-read'),
        async: true,

        callback: function(opts, success, response) {
            try {

                if (success) {
                    var output = App.decodeHttpResp(response.responseText);
                    const data = output.data;

                    let myArr = [];
                    data.map((date) => 
                    myArr = Object.keys(date).filter(key => key != 'DATE'));
                    me.hrer =myArr;

                    console.log(me.hrer);

                    if (output.success) {
                        //return output.success;
                    } else {
                        //return output.error;
                    }
                } else {
                    //return 'Unknown Reason';
                }
            } catch (ex) {
                //return ex;
            }
        }
    });
console.log(me.hrer);

我想把hrer数组带到外面。当我在ajax请求之外的console.log时,我得到一个我已定义为全局的空数组。我无法将进入ajax请求的值传递到外部

1 个答案:

答案 0 :(得分:0)

在ExtJS 4中,我们曾经做过:

Ext.define('A.store.SharedData', { 
    singleton: true, 

    foo: 'bar', 
    johnDoeAge: 42,
    hrer: []
}); 

然后在您想要使用的任何组件中使用它并使用它:

Ext.define('A.view.Management', {
    extend: 'Ext.container.Container',

    requires: [
        'A.store.Management',
        'A.store.SharedData'
    ],




    initComponent: function () {
        var me = this;
        A.store.SharedData.hrer = [1, 2, ...];
        ...
    }
    ...
});