I have a list of geographic coordinates in my application, stored in a Vars[Coordinates]
. I'd now like to display these on a map and automatically update the map when new coordinates are added to or removed from the list. I'm using a library that exposes APIs to add and remove markers on a map, so I'd like to invoke these when the list is updated, but I can't find any obvious way to do that. Any hints on how to achieve this?
edit: Thanks @ Yang Bo for responding! I ended up with something like this:
val coordinates = Vars.empty[Coordinates]
def mapMountPoint(parent: Element, coordinates: BindingSeq[Coordinates]) =
new MultiMountPoint[Coordinates](coordinates) {
… // method overrides here to create the map in parent
}
@dom
def map = {
val e = <div></div>
mapMountPoint(e, coordinates).bind
e
}
It appears to work, the mount and unmount methods are called when the div is rendered or removed from the DOM… But is this really how it's supposed to be done? It looks a bit weird, and I also get a compiler warning where I call .bind
: a pure expression does nothing in statement position; multiline expressions might require enclosing parentheses
.
In the monadic-html library, there is a rather elegant way to do it:
<canvas mhtml-onmount={ e => crazyCanvasStuff(e) }></canvas>
There is also a matching mhtml-onunmount attribute for cleanup.