我的应用程序中有一个地理坐标列表,存储在Vars[Coordinates]
. 我现在想在地图上显示这些,并在将新坐标添加到列表中或从列表中删除时自动更新地图。我正在使用一个库来公开 API 以在地图上添加和删除标记,所以我想在列表更新时调用这些,但我找不到任何明显的方法来做到这一点。关于如何实现这一目标的任何提示?
编辑:感谢@杨波的回复!我最终得到了这样的结果:
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
}
它似乎工作,当 div 被渲染或从 DOM 中删除时调用 mount 和 unmount 方法……但这真的是应该这样做的吗?它看起来有点奇怪,而且我在调用.bind
:的地方也收到了编译器警告a pure expression does nothing in statement position; multiline expressions might require enclosing parentheses
。
在 monadic-html 库中,有一种相当优雅的方法:
<canvas mhtml-onmount={ e => crazyCanvasStuff(e) }></canvas>
还有一个匹配的 mhtml-onunmount 属性用于清理。