问题
我正在使用 leaflet draw对于我的应用程序,我激活了“删除”按钮。删除按钮具有三个选项:
- 保存
- 取消
- 全部清除
我希望函数 foo()
在用户点击 Save 时被调用,但是,我希望函数 bar()
被调用他们应该点击取消吗?
解决方案
我知道这可以通过简单地给它一个 ID 并添加一个事件监听器来实现,但它并不像我认为的那样干净。
理想的解决方案
Leaflet 绘制了自己的方法来检测按钮何时被按下,但在我看来他们只在更高一级上这样做。例如:
draw:deletestop
The type of edit this is. One of: remove Triggered when the user has finished removing shapes (remove mode) and saves.
这允许我在用户选择了三个选项中的任何之后调用 foo()
,呈现他们已经简单地完成了删除按钮交互的处理。
我无法在文档中找到一种方法来监听在按下各个按钮时触发事件的传单绘制。
最佳答案
取消/禁用函数的处理程序存储为 L.Control.Draw
实例的一部分。因此,您可以在实例化 L.Control.Draw
对象后立即修改处理程序:
var myDrawControl = new L.Control.Draw();
myDrawControl._toolbars.edit.disable = function () {
if (!this.enabled()) {
/* If you need to do something right as the
edit tool is enabled, do it here right
before the return */
return;
}
this._activeMode.handler.revertLayers();
/* If you need to do something when the
cancel button is pressed and the edits
are reverted, do it here. */
L.Toolbar.prototype.disable.call(this);
};
处理程序的来源是 here虽然这很有效,但您必须小心 Leaflet.Draw 的 future 版本,它可能会改变处理程序的功能。
关于javascript - 识别何时单击 Leaflet Draw 取消按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45839502/