diff options
Diffstat (limited to 'mavigator-cockpit/src/main/scala/mavigator/index/Util.scala')
-rw-r--r-- | mavigator-cockpit/src/main/scala/mavigator/index/Util.scala | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/mavigator-cockpit/src/main/scala/mavigator/index/Util.scala b/mavigator-cockpit/src/main/scala/mavigator/index/Util.scala new file mode 100644 index 0000000..16f3d5d --- /dev/null +++ b/mavigator-cockpit/src/main/scala/mavigator/index/Util.scala @@ -0,0 +1,39 @@ +package mavigator.index + +import scala.language.implicitConversions + +import org.scalajs.dom.html + +import rx._ + +import scala.util.Try +import scala.util.Success +import scala.util.Failure + +import scalatags.JsDom.all._ + +object Util { + + /** + * Copied from https://github.com/lihaoyi/workbench-example-app/blob/todomvc/src/main/scala/example/Framework.scala + * + * Sticks some Rx into a Scalatags fragment, which means hooking up an Obs + * to propagate changes into the DOM via the element's ID. Monkey-patches + * the Obs onto the element itself so we have a reference to kill it when + * the element leaves the DOM (e.g. it gets deleted). + */ + implicit def rxMod[T <: html.Element](r: Rx[HtmlTag]): Frag = { + def rSafe = r.toTry match { + case Success(v) => v.render + case Failure(e) => span(e.toString, backgroundColor := "red").render + } + var last = rSafe + Obs(r, skipInitial = true) { + val newLast = rSafe + last.parentElement.replaceChild(newLast, last) + last = newLast + } + bindNode(last) + } + +} |