【问题标题】:pass the argument from one method to another method in extjs将参数从一个方法传递到 extjs 中的另一个方法
【发布时间】:2018-02-13 12:47:13
【问题描述】:

这是我的观点

Ext.define('A2a.view.act.Job', { 扩展:'Ext.container.Container',

requires: [
    'A2a.store.Job',
    'A2a.store.SharedData',
],

border: false,
chart: null,




hrer: [],
layout: {type: 'vbox', pack: 'start', align: 'stretch'},



initComponent: function () {
    var me = this;


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

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

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


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

                    console.log(me.hrer =myArr);

                    me.loadChart();
                    //me.loadww();

                } 
            } catch (ex) {
                //return ex;
            }
        }
    });



   this.loadww();
    this.loadData(me.hrer);

    me.callParent(arguments);


},
loadData : function () {
    console.log("Init Function");
},







loadww: function (hrer) {
    var me = this;
    //var self = this;
    console.log(me.hrer);
    me.jobStore = Ext.create('O2a.store.PendingReports');
    Ext.apply(me, {
        items: [
            {
                xtype: 'chart',
                itemId: 'charid',
                name: 'charid',
                store : new Ext.create('Ext.data.JsonStore', {
                        proxy: {
                            type: 'ajax',
                            url : utils.createUrl('api', 'dashboard-read'),
                            reader: {
                                type: 'json',
                                root: 'data'
                            }
                        },
                        autoLoad : true,
                        successProperty : 'success',

                        fields : [{name: 'DATE', type: 'auto'}].concat(

    O2a.store.SharedData.hrer.map(function(companyName) {
        console.log(companyName);
        return {
            name: companyName,
            type: 'int'
        };
    })
)
                    }),
                style: 'background: #fff',
                insetPadding: 40,
                animate: true,
                shadow: false,
                flex: 2,
                minHeight: 300,
                legend: {
                    position: 'top',
                    boxStrokeWidth: 0,
                    labelFont: '12px Helvetica'
                },

                axes: [{
                        type: 'Numeric',
                        position: 'left',
                        fields: ['1'],
                        grid: true,
                        minimum: 0,

                    }, {
                        type: 'Category',
                        position: 'bottom',
                        fields: ['DATE'],
                        grid: true,
                    }],

            }]
    });
},



loadChart: function (hrer) {

    var me = this;
    console.log(me.hrer);
var cha = this.down('#charid');
var iii = null;
    Ext.Ajax.request({
        url: utils.createUrl('api', 'dashboard-read'),
        async: true,

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

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

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


cha.series.clear();
    for(var i=0;i<myArr.length;i++){

    cha.series.add({           
    type: 'line',
    axis: 'left',
    xField: 'DATE',
    border: false,
    flex: 1,
    title: myArr[i],
    yField: myArr[i],       
    markerConfig: {
        radius: 4
    },
    highlight: {
        fill: '#000',
        radius: 5,
        'stroke-width': 2,
        stroke: '#fff'
    },

    tips: {
        trackMouse: true,
        style: 'background: #FFF',
        height: 20,
        width: 120,

        renderer: function (storeItem, item) {
            var name = item.series.title[Ext.Array.indexOf(item.series.yField, item.yField)];
            this.setTitle(name + ': ' + storeItem.get(item.yField));
        }
    }


    });
}
            } else {
                    //return 'Unknown Reason';
                }
            } catch (ex) {
                //return ex;
            }
        }
    });

}

} );

我想在 loadww 方法中传递 hrer。如果我在 initcomponent 函数的回调中调用 loadww,我就可以通过它。但是图表没有加载。如果我在 ajax 请求之后调用它,图形正在加载,但不能将 hrer 传递到外部。
如何在 loadww 函数中传递 hrer 数组。提前致谢

【问题讨论】:

  • 如果 loadww 需要请求回调填充到 hrer 中的数据,那么在 Ext.Ajax.request 之后立即调用它是行不通的。这是因为请求是异步的,所以保证在请求回调执行之前调用loadww。因此,需要从回调中调用 loadww。但是,提供的代码是不完整的,所以我不能说为什么图表没有加载。
  • 感谢您的帮助。我用完整的代码编辑了我的帖子。你能否指导我为什么图表没有加载。我努力了。但不能。提前致谢

标签: javascript extjs parameter-passing


【解决方案1】:

从代码的风格来看,这是 ExtJS 3.x 在 Ext.Ajax.request 中用 this 定义范围以留在视图中。

您的代码不是最佳状态。

  • 摆脱 try...catch。
  • 尝试写入成功:this.dashboardSuccess
  • 使用回调,将在两种情况下运行(成功、失败)

如果这是 ExtJS 4+,你应该在 MVC 中拆分你的代码

如果这是 ExtJS 6+,你应该在 MVVM 中拆分你的代码

  • 编写声明式代码
  • 在 VM 中取出存储并使用加载事件
  • 不要使用initComponent
  • 使用数据绑定

【讨论】:

    猜你喜欢
    • 2016-12-21
    • 2019-03-02
    • 1970-01-01
    • 2012-04-11
    • 1970-01-01
    • 2014-02-24
    • 1970-01-01
    • 2018-08-08
    相关资源
    最近更新 更多