2

I am loading google map in async way ,

@JSExport("sample")
 object Sample {

  def loadScript = {
    val script = document.createElement("script").asInstanceOf[HTMLScriptElement]
    script.`type` = "text/javascript"
    //case 1
    script.src = "https://maps.googleapis.com/maps/api/js?v=3.exp&callback=sample().initialize"
    // case 2
    script.src = "https://maps.googleapis.com/maps/api/js?v=3.exp&callback=sample.initialize"
    document.body.appendChild(script)
  }

  @JSExport
  def initialize() :Unit  = {
     println(" map loaded successfully")
  }
}

In case 1 google sending response - 400(bad request)

In case 2 i am getting undefined function ( window.sample.initialize())

i can define a javascript function ,inside that function i can call sample().initialize() , but is there any cleaner way ?

3
  • IMHO case 1 should be right. Are you sure this is not just a URL encoding problem? Commented Dec 12, 2014 at 9:45
  • no URL encoding issues,:host:maps.googleapis.com :method:GET :path:/maps/api/js?v=3.exp&callback=sample().initialize Commented Dec 12, 2014 at 9:52
  • 1
    Thanks for bringing this up. FYI I filed an issue for this: github.com/scala-js/scala-js/issues/1381 Commented Dec 12, 2014 at 9:56

2 Answers 2

4

I would use Scala.js' dynamic API to create the JavaScript function on the top-level. The advantage over @gzm0's solution is that it's less hacky, and requires less boilerplate.

object Sample {
  def loadScript = {
    val script = document.createElement("script").asInstanceOf[HTMLScriptElement]
    script.`type` = "text/javascript"
    script.src = "https://maps.googleapis.com/maps/api/js?v=3.exp&callback=initializeSample"
    document.body.appendChild(script)

    js.Dynamic.global.initializeSample = initialize _
  }

  private def initialize(): Unit =
    println("map loaded successfully")
}
Sign up to request clarification or add additional context in comments.

2 Comments

thanks a ton man it solved my other issue too :) ,btw what is that _ ?
It converts the method into a Function. See for example stackoverflow.com/questions/8000903/…
1

This is a hacky answer, but potentially useful as a workaround.

Instead of giving the Google API something corresponding to a Scala.js function, you can give it the module initializer directly:

object Sample {
  def loadScript = {
    val script = document.createElement("script").asInstanceOf[HTMLScriptElement]
    script.`type` = "text/javascript"
    script.src = "https://maps.googleapis.com/maps/api/js?v=3.exp&callback=initializeSample"
    document.body.appendChild(script)
  }
}

@JSExport("initializeSample")
object Initializer {
  println(" map loaded successfully")
}

1 Comment

thank you ,its definitely a good work around than creating javascript function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.