динамические ключи сопоставления клавиш extjs

#extjs #keymapping

#extjs #сопоставление клавиш

Вопрос:

Привет, у меня это реализовано, и я хочу, чтобы ключи были динамическими на основе возврата функции. Допустим, у меня есть функция в контроллере под названием «returnKeyFront», которая возвращает клавишу «F». Затем я хочу взять его и применить к первому элементу F. Поэтому вместо записи F была бы функция, которая возвращает этот ключ. Понятия не имею, как это реализовать. Я использую клавиатурную карту из ExtJS с coffescript. тнх

 keyMap:   F:   handler: "onCamerasHotkeyFront"   B:   handler: "onCamerasHotkeyBack"   

Ответ №1:

Вы можете добавить вспомогательную функцию.

Что-то вроде этого (я не проверял это, но должен быть довольно хороший намек.

 Ext.define('MyApp.KeyChain', {  singleton: true,   /**  * holds the current keymap appwide  * @private  */  _keymap: null,   /**  * initialize with a base defintion   */  initKeyChain() {  const keyChain = this.definition;   this.updateKeyMap();  },   /**  * base definition. You can also set it to null  * if you want to start blank  *  * @private  */  definition: {  CameraFront: {  key: 'f',  alt: true,  fn: 'onCamerasHotkeyFront',  scope: this // typically you want to either set the fn and/or scope  },  CameraBack: {  key: 'f',  alt: true,  fn: 'onCamerasHotkeyBack',  scope: this // typically you want to either set the fn and/or scope  },  },   /**  * changeKey  * to add or change a definition. You can also init via changeKey  * see this.defintion for examples on how keyDefinition should look like  *  * @arg {string} type e.g. 'CameraFront'  * @arg {object} keyDefinition  * @arg {string} keyDefinition.key  * @arg {boolean} [keyDefinition.alt]  * @arg {function|string} keyDefinition.fn  * @arg {scope} [keyDefinition.scope]  */  changeKey(type, keyDefinition) {  if(!keyDefinition.scope) keyDefinition.scope = this;  if(typeof keyDefinition.fn === 'string') keyDefinition.fn = scope[definition.fn];   MyApp._definition[type] = keyDefinition;  this.updateKeyMap();  },   /**  * updateKeyMap  *   * @private  */  updateKeyMap() {  if(this._keymap) this._keymap.destroy();   const newBinding = this.createBindings();   this._keymap = new Ext.util.KeyMap({  target: Ext.getBody(),  binding: newBindings  });  },   /**  * createBinding  *   * @private  */  createBinding() {  const def = this.definition;  let bindings = [];   Ext.Object.each(def, function(key, value) {  bindings.push({value});  });   return bindings;  } });