Search code examples
scalascala.jsgoogle-picker

Google Picker using Scala.js


I'd like to use Google Picker in my Scala.js app. I need to convert this example somehow.

There are two main questions:

The first, how can I load and use https://apis.google.com/js/api.js in my Scala.js code to use gapi object?

gapi.load('auth', {'callback': onAuthApiLoad});
gapi.load('picker', {'callback': onPickerApiLoad});

The second, after a picker will be loaded, how can I access to the google.picker object to create the picker?

var picker = new google.picker.PickerBuilder()

Solution

  • As for any other JavaScript API, you can basically access it with a dynamically typed API using js.Dynamic or with a (possibly hand-written) typed facade.

    In this case, I'd recommend the dynamic API for the first part:

    import scala.scalajs.js
    import js.Dynamic.{global => g, literal => lit}
    
    g.gapi.load("auth", lit(callback = onAuthApiLoad))
    g.gapi.load("picker", lit(callback = onPickerApiLoad))
    

    and a static API for the second part:

    @js.native
    @JSName("google.picker.PickerBuilder")
    class PickerBuilder() extends js.Object {
      // here you can declare methods and fields of PickerBuilder
    }
    
    val picker = new PickerBuilder()