Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
385 views
in Technique[技术] by (71.8m points)

javascript - Kotlin JavaScript removeEventListener本身(Kotlin JavaScript removeEventListener from itself)

I'm stuck on a simple drag-and-drop functionality using Kotlin JS.

(我只能使用Kotlin JS进行简单的拖放功能。)

I add a handleDrag event listener and then I want to remove it in handleMouseUp listener.

(我添加了handleDrag事件侦听器,然后在handleMouseUp侦听器handleMouseUp其删除。)

That works perfectly.

(那很好。)

The thing is that handleMouseUp also needs to remove itself.

(问题是handleMouseUp也需要删除自身。)

It's quite straightforward in JS but not so much in Kotlin apparently.

(在JS中,这非常简单,但在Kotlin中却没有那么多。)

val handleMouseUp = fun(evt: Event) {
    evt as MouseEvent
    GUI.removeListener("mousemove", handleDrag)
    GUI.removeListener("mouseup", handleMouseUp) //unresolved reference: handleMouseUp
}
GUI.addListener("mousemove", handleDrag)

I also tried named functions;

(我还尝试了命名函数;)

that compiles but doesn't remove the listener for some reason (probably two references like ::handleMouseUp aren't the same object).

(可以编译,但由于某种原因不会删除侦听器(可能像::handleMouseUp这样的两个引用不是同一对象)。)

Is there any way to do this the way I intended at the beginning?

(有什么方法可以按照我一开始的意图进行?)

If not I accept tips how to work around it.

(如果没有,我接受如何解决的提示。)

Maybe I'm missing something because I'm not experienced in event-driven and JS programming.

(也许我错过了一些东西,因为我没有事件驱动和JS编程方面的经验。)

removeListener and addListener are just wrappers

(removeListeneraddListener只是包装器)

fun addListener(type: String, listener: (Event) -> Unit) {
    canvas.canvas.addEventListener(type, listener)
}
fun removeListener(type: String, listener: (Event) -> Unit) {
    canvas.canvas.removeEventListener(type, listener)
}
  ask by Adam Stafiej translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...