mercredi 9 mars 2016

Writing a service with an external dependancy

I am writing a service to wrap a couple of Google Places API calls that I need for my app.

Something like:

import Ember from 'ember';

export default Ember.Service.extend({
  googleAutocompleteService: null,
  placePredictions(query) {
    var self = this;
    return new Ember.RSVP.Promise(function(resolve, reject){
      ...
      self.get('googleAutocompleteService').getPlacePredictions(request, callback);
    });
  },
  init(){
    this._super(...arguments);
    this.set('googleAutocompleteService',
             new google.maps.places.AutocompleteService());
  },
});

Now I obviously need to have the Google Maps API loaded. For the moment I have included it in my index.html:

<!DOCTYPE html>
<html>
  <head>
    ...
  </head>
  <body>
    {{content-for 'body'}}

    <script src="http://ift.tt/24Qsm1b" async defer></script>

    ...

    {{content-for 'body-footer'}}
  </body>
</html>

Occasionally my service will load before the maps API is initialized causing an error. I can simply remove async defer from the tag and load it synchronously, but there must be a way to have my service load the external library as a dependency?




Aucun commentaire:

Enregistrer un commentaire