These are chat archives for ThoughtWorksInc/Binding.scala

23rd
Nov 2018
Mustafa Kuscu
@mcku
Nov 23 2018 11:09
One more question. Let's say we have a single page app (SPA) and would like to use a legacy library that is jQuery based, such that some random elements need to be initialized by some jQuery plugin. The SPA renders totally different layouts depending on some conditions (like authentication), therefore it is not guaranteed after the first page load the elements are attached to the DOM. Is there a way to attach an initializer to a div just for this purpose, so that wherever the div is being used, it is going to get the initializer invoked? (If you know an answer and are willing to answer on SO, I can enter a question there, too.)
Mustafa Kuscu
@mcku
Nov 23 2018 11:19
Just to make things clear, even workarounds like this after where dom.render() gets called did not help
def applyOnMount(selector: String, f: () => Unit):Unit = {
    val elemFound: Boolean = org.scalajs.dom.document.body.querySelectorAll(selector).length > 0
    if (!elemFound) {
      println(s"waiting for elem $selector")
      js.timers.setTimeout(Duration.seconds(1000).milliseconds) {
        println(s"recursing for $selector")
        applyOnMount(selector,f)
      }
    } else {
      println(s"applying on elem $selector")
      f
    }
  }
Kahli Burke
@kahliburke
Nov 23 2018 17:16
I’d investigate
杨博 (Yang Bo)
@Atry
Nov 23 2018 17:27
Have you search the answer of your question on StackOverflow?
Mustafa Kuscu
@mcku
Nov 23 2018 19:46
Thanks for the recommendation, @kahliburke, although i was trying to stay within the binding.scala tooling like watch(),.. Would it be possible to implement it using watch()? I have not tested MutationObserver before, i will consider using it instead of re-implementing some existing components in binding.scala.
Mustafa Kuscu
@mcku
Nov 23 2018 19:54
@Atry got your point. A similar example I was following was @sadhen's here: http://sadhen.com/blog/2017/01/02/binding-with-semantic.html
Mustafa Kuscu
@mcku
Nov 23 2018 20:03
When things build up including routing, ui navigation, authentication, working with local and remote data and futurebindings, social media logins, google/leaflet maps, etc, some sort of best practices become necessary. It may be a good idea to keep a binding.scala starter project with up to date best practices for reference and building shared experience. I don't mind offering a starter skeleton for going through common scrutiny if it helps...