mardi 17 novembre 2015

Emberjs Services and 3rd Party API that need to be transformed

Current Situation
- I have access to a 3rd Party API I do not own
- The data from the API needs to be filtered down to a much smaller list
- I will need this smaller list in other areas of the app

The service is a list of countries that I want to make available in multiple areas of the app. I think having this as a service is good idea but I’m running into problems.

// services/countries.js

export default Ember.Service.extend({

    promise: function() {
        return Ember.$.getJSON(‘/data/third-party-api.json’);
    }

    all: function() {
        return this.get(‘promise’);
    }

});

export default Ember.Route.extend({

    countries: Ember.inject.service(),

    model:function() {
      return Ember.RSVP.hash({
        countries: this.get(‘countries’).all()
      })
    }
});

What I want to do is filter, parse the JSON into a smaller list of new JSON in the service file. Like this.

export default Ember.Service.extend({

promise: function() {
    return Ember.$.getJSON(‘/data/third-party-api.json’);
}

all: function() {
    let promise = this.promise

    return _.chain(promise)
        .groupBy(‘topic’)
        .omit(‘undefined’)
        .value();
}
})

Problem
• The filter of the RAW JSON in the all function() is not working in the service file.
• If I run this filtering code in the model function within IndexRoute it will parse and filter and it will work BUT I need it to work in the service because I want to share this within the app.

Maybe Ember Services is not the right place? Or do I need to run some Ember hook to wait in the services file.

Aucun commentaire:

Enregistrer un commentaire