lundi 10 septembre 2018

Using fetch inside an action within my component

I'm curious about how I could implement this, I'd like to not hit this API every time the page loads on the route, but would rather start the call on an action (I suppose this action could go anywhere, but it's currently in a component). I'm getting a server response, but having trouble getting this data inside my component/template. Any ideas? Ignore my self.set property if I'm on the wrong track there....Code below..Thanks!

import Component from '@ember/component';

export default Component.extend({
  res: null,
  actions: {
    searchFlight(term) {
      let self = this;
      let url = `https://test.api.amadeus.com/v1/shopping/flight-offers?origin=PAR&destination=LON&departureDate=2018-09-25&returnDate=2018-09-28&adults=1&travelClass=BUSINESS&nonStop=true&max=2`;
      return fetch(url, {
        headers: {
          'Content-Type': 'application/vnd.amadeus+json',
          'Authorization':'Bearer JO5Wxxxxxxxxx'
        }
      }).then(function(response) {
        self.set('res', response.json());
        return response.json();
      });
    }
  }
});




Aucun commentaire:

Enregistrer un commentaire