Ace Editor AddCommand -如何调用外部函数



我正在开发的web应用程序中使用Ace编辑器。我有一个保存按钮,它调用我写的函数handleFileUpdate()。但是,我还希望用户能够通过键盘快捷键保存内容。

下面是我当前的Ace键绑定代码:

this.editor.commands.addCommand({
name: 'save',
bindKey: {win: "Ctrl-S", "mac": "Cmd-S"},
exec: function(editor) {
this.handleFileUpdate();
}
});

其中this.handleFileUpdate()在我自己的代码中是一个函数。

当命令被调用时,Ace抛出以下错误:

TypeError: this.handleFileUpdate is not a function. (In 'this.handleFileUpdate()', 'this.handleFileUpdate' is undefined)

我明白this.handleFileUpdate()对Ace是不可见的。

我的问题是:Ace命令怎么可能调用外部函数?或者换句话说,如何为Ace提供自定义函数,以便在按下Ctrl-S时调用它?

这与其说是一个关于ace的问题,不如说是关于javascript中的这个处理。

在你的例子中,内部函数中的this是定义方法的对象。

作为一种解决方法,您可以使用箭头函数。

this.editor.commands.addCommand({
name: 'save',
bindKey: {win: "Ctrl-S", "mac": "Cmd-S"},
exec: (editor) => {
this.handleFileUpdate();
}
})
``~

最新更新