gpt4 book ai didi

extjs - 将 KeyMap 添加到视口(viewport)的最佳实践

转载 作者:行者123 更新时间:2023-12-02 08:30:28 26 4
gpt4 key购买 nike

创建/绑定(bind) KeyMaps 到视口(viewport)的最佳位置是什么?

给定一个像这样的非常简单的视口(viewport):

Ext.define('EmptyTemplate.view.Viewport', {
extend: 'Ext.container.Viewport',
requires:[
'Ext.layout.container.Fit',
'EmptyTemplate.view.Main'
],
layout: {
type: 'fit'
},

items: [{
xtype: 'app-main'
}],

listeners: {
afterrender: {
fn: function(){
// map one key by key code
this.keyMap = Ext.create('Ext.util.KeyMap', this.el, {
scope: this,
key: Ext.EventObject.ENTER,
fn: function () {
console.log("enter pressed");
}
});
}
}
}
});

创建 KeyMap 的正确方法是什么?

最佳答案

首先是一些最佳实践建议:

如果你需要设置你的组件使用

  • [initComponent][1](您应该 read this 了解详细信息),
  • 其他提供的模板方法
  • 在极少数情况下,构造函数

在你的情况下,我会使用模板方法 afterRender

Ext.define('EmptyTemplate.view.Viewport', {
extend: 'Ext.container.Viewport',
requires:[
'Ext.layout.container.Fit',
'EmptyTemplate.view.Main'
],
layout: {
type: 'fit'
},

items: [{
xtype: 'app-main'
}],

afterRender: {
this.callParent(arguments); // always!!
this.bindKeyMap();
},

bindKeyMap: function() {
var me = this; // use 'me' if 'this' occurs more then 3 times
if(me.keyMap) {
me.keyMap.enable();
return;
}
// map one key by key code
me.keyMap = Ext.create('Ext.util.KeyMap', me.el, {
scope: me,
key: Ext.EventObject.ENTER,
fn: me.onEnter
});
},

unbindKeyMap: function() {
this.keyMap.disable();
},

onDisable: function() {
this.unbindKeyMap();
this.callParent(arguments); // always!!
},

onEnable: function() {
this.callParent(arguments); // always!!
this.bindKeyMap();
},

onEnter: function(){
// i am executed in the scope of the class instance
}
});

请注意,上面的示例处理了整个键盘映射,但您也可以 add / remove map 中的单个键。

Note that this is untested prototype code, but it should work this way.


如何查找模板方法:

  1. 转到 docs
  2. 显示 protected 成员

enter image description here

  1. 寻找 enter image description here标记

enter image description here

This post about overriding也可能是一本好书

关于extjs - 将 KeyMap 添加到视口(viewport)的最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27463675/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com