Search code examples
ffireasonbucklescript

Create binding based on external JS


In this post, the autor teaches how to make a binding from a NodeJS library to Reason. However, I want to create a binding for Google Maps Javascript API, which can't be installed through NPM. Rather, it's usually loaded in the bottom of <body> with <script> tag.

Also, Google Maps Javascript API only exports it's functions inside the function that has been passed as argument in url (callback=funcName). Will this work in Reason the same way as in raw JS?

How can I make this binding?


Solution

  • The API is installed as a global, so you'd just bind to them as ordinary globals. And since Reason functions generate ordinary JavaScript functions the following is more or less equivalent to the example in the documentation you've linked:

    type map;
    [@bs.new] [@bs.scope ("google", "maps")] external make : (Dom.element, Js.t({..})) => map = "Map";
    
    let initMap = () => {
      let map = make(mapElement, {
        "center": { "lat": -34.397, "lng": 150.644 },
        "zoom": 0
      });
    };