-->

如何在EXTJS调用的视听者从控制器(how to call a listener of view

2019-10-18 03:14发布

我有我的观点柱状图在那里我有被检索条列项的听众,现在我要调用的侦听器控制器这里是我的代码...

这是在我看来听众..

listeners: {
    itemmousedown: function (obj) {
        alert(obj.storeItem.data['source'] + ' &' + obj.storeItem.data['count']);
    }
},

和我有打电话给从我controller.here这个监听器是我的代码..

init: function () {
    this.control({
        'barColumnChart': { //this is the id of the bar chart in my View
            click: function () {

            }
        }
    });
},

Answer 1:

该itemmousedown事件是由“串联”解雇,该系列是不是一个组成部分,并且只通过布局后的图表初始化。 因此,为了得到我们需要等待图表afterlayout事件在系列的对象的引用。 但不幸的是图表在不触发一个事件afterlayout ......所以,首先覆盖图表的afterComponentLayout方法,使得其触发事件:

Ext.define('MyNewChart',{
    extend: 'Ext.chart.Chart',
    afterComponentLayout: function(width, height, oldWidth, oldHeight) {
        this.callParent(arguments);
        this.fireEvent('afterlayout', this);
    }
});

现在,使用我们的新图表类来创建图表:

Ext.create('MyNewChart', {
     id: 'myChart',
     ...
});

现在我们可以创建实际创建的itemmousedown事件的侦听器控制器:

Ext.define('Gamma.controller.ControlFile', {
    extend : 'Ext.app.Controller',
    initializedEvents: false,
    init: function() {
        this.control({
            '#myChart': {
                afterlayout: this.afterChartLayout
            }
        });
    },
    afterChartLayout: function(){
        if(this.initializedEvents==true) return;
        this.initializedEvents=true;
        Ext.getCmp('myChart').series.items[0].on('itemmousedown',function(obj){
            console.log(obj);
        });
    }
});

这里是工作提琴: http://jsfiddle.net/qjfBC/



Answer 2:

您是否尝试过使用“#barColumnChart”的选择? 我有点生疏,但我认为这是你由它的控制器内部ID获得元素的方法。



Answer 3:

你不“呼”的监听器,当事件被触发的监听器被调用。 所以,你应该设置控制器内部的itemmousedown监听器,并从视图中移除。 我不知道哪种说法有itemmousedown事件,但如果它确实是条形图,它应该是这样的:

this.control({
    '#barColumnChart': { //this is the id of the bar chart in my View
        itemmousedown: function(obj) {
            alert(obj.storeItem.data['source'] + ' &' + obj.storeItem.data['count']);
        }
    }
});

但是,如果该事件是另一种意见认为,应更换“#barColumnChart”与正确政绩观的ID(或其他选择该视图)



Answer 4:

如果您所创建的侦听到控制器,你就不必创建到视图中。
在控制器中,你可以创建你的看法是这样的引用:

refs: [{
    ref     : 'barColumnChart',
    selector: 'your_view'
}
   }]  

然后创建将在项目鼠标按下要调用的函数:

me.control({
'barColumnChart#your_chart': {
            click: me.your_function
        }
}),

your_function(button, e, options) {
        alert(obj.storeItem.data['source'] + ' &' + obj.storeItem.data['count']);
    }


文章来源: how to call a listener of view from controller in Extjs