mercredi 24 février 2016

Using callbacks in a non-async-context

I have a service with a method called "getGmapsDistance()". Here im using the google maps api to get the distance between an origin an an destination.

export default Ember.Service.extend({

  getShortestDistanceInMeters: function(location) {

    var service = new google.maps.DistanceMatrixService();

    service.getDistanceMatrix({
      ...
    }, this.callback); //<<<<<< !!!

  },

  callback: function(response, status) {
      ....
  }
});

In my controller if got a array with locations and now I want to iterate over it and want check each element if the distance is <= the max destination.

locationsNearby: Ember.computed('locations', function() {
     //...
     var filteredResult = [];

      locations.forEach(function(locat) {
        if (this.get('distanceService').getShortestDistanceInMeters(locat) <= maxDistance) {
          filteredResult.pushObject(locat);
        }
      });

      return filteredResult;
})

Unfortunately the GMaps API for distance calculation uses a callback so the request is async.

How can I solve that problem?




Aucun commentaire:

Enregistrer un commentaire