我继承了一个庞大的ExtJS3代码库,并且有一个“base”覆盖Ext.grid.CellSelectionModel的beforecellselect。我正在截断大量的代码,但这应该提供一般的想法:
Ext.override(Ext.grid.CellSelectionModel, {
init:function() {
Ext.grid.CellSelectionModel.superclass.init.apply(this, arguments);
if (this.unselectableColumns || this.visuallyMimicRowSelection || this.colSpecificHandlers){
this.on('beforecellselect', function(selModel, rowIndex, columnIndex){
//etcetera
然后,我们将实例化一个CellSelectionModel,并在其上指定beforecellselect侦听器,如下所示:
var sm = new Ext.grid.CellSelectionModel({
listeners: {
beforecellselect : {
fn: function(selModel, rowIndex, colIndex) {
//etcetera
问题是,在新的CellSelectionModel实例的侦听器中,我还需要调用覆盖中定义的侦听器。因为ExtJS似乎保留了一组同名的事件监听器,所以我可以按如下方式委托:
selModel.events.beforecellselect.listeners[1].fn.apply(selModel, arguments);
好的,我知道我不应该对索引进行硬编码。但除此之外,还有更好的,更多的ExtJS-y方式吗?
答案 0 :(得分:1)
在你的情况下,如果你知道它将是一个将在构造函数之外使用的函数, 我建议添加事件处理函数作为CellSelectionModel的方法 实例,如下所示:
Ext.override(Ext.grid.CellSelectionModel, {
init:function() {
Ext.grid.CellSelectionModel.superclass.init.apply(this, arguments);
this.customBeforeCellSelect = function(selModel, rowIndex, colIndex) {
// etcetera
};
if (this.unselectableColumns
|| this.visuallyMimicRowSelection
|| this.colSpecificHandlers) {
this.on('beforecellselect', this.customBeforeCellSelect, this);
}
});
var sm = new Ext.grid.CellSelectionModel({
listeners: {
beforecellselect : {
fn: function(selModel, rowIndex, colIndex) {
selModel.customBeforeCellSelect.apply(selModel, arguments);
},
scope: sm
}
}
});
但是,请记住,您在重写的构造函数中将事件处理程序附加到beforecellselect事件,因此如果您在特定实例listeners
中的beforecellselect期间再次调用此事件处理函数,那么您将结束连续两次执行相同的功能。
出于效率考虑,您可以将自定义处理程序移动到Ext.grid.CellSelectionModel
的原型,即,而不是将customBeforeCellSelect
放在init
内的单个实例上。为此,请执行以下操作:
Ext.grid.CellSelectionModel.prototype.customerBeforeCellSelect =
function(selModel, rowIndex, colIndex) {
// etcetera
};
您可以在override
声明之后添加上述行。