Last active
May 20, 2016 21:36
-
-
Save sjrd/592af1a5b41d553b73d5a82280e58b64 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package example | |
import scala.scalajs.js | |
import org.scalajs.dom | |
@js.native | |
trait EventName extends js.Any { | |
type EventType <: dom.Event | |
} | |
object EventName { | |
def make[T <: dom.Event](name: String): EventName { type EventType = T } = | |
name.asInstanceOf[EventName { type EventType = T }] | |
val mousedown = make[dom.MouseEvent]("mousedown") | |
} | |
@js.native | |
trait ElementExt extends js.Object { | |
def addEventListener(name: EventName)( | |
f: js.Function1[name.EventType, _]): Unit | |
} | |
object ScalaJSExample extends js.JSApp { | |
def main(): Unit = { | |
val paragraph = dom.document.createElement("p") | |
paragraph.innerHTML = "<strong>It works! (click me)</strong>" | |
dom.document.getElementById("playground").appendChild(paragraph) | |
val p = paragraph.asInstanceOf[ElementExt] | |
p.addEventListener(EventName.mousedown) { (e: dom.MouseEvent) => | |
println("clicked " + e.button) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment