mercredi 16 novembre 2016

Ember.js linking application to real api after mirage success

As I'm going deeper and deeper into Ember.js application building process I hit another wall.

Before I was using mirage with great success - I just copy output from API that I wanted build around to mirage fixtures and it was working great.

Now I have problem with making it work with real API.

I first disabled mirage in config/environment.js

ember g adapter filter

import ApplicationAdapter from './application';
export default ApplicationAdapter.extend({
    findAll: function(store, type, label) {
        var url = `${this.host}/${this.namespace}/${type.modelName}`;
        console.log(`${url}`);
        return this.ajax(url, 'GET');
    },
});

The application adapter looks like this

import DS from 'ember-data';

export default DS.JSONAPIAdapter.extend({
    host: 'http://127.0.0.1:1234',
    namespace: 'api',

    headers: Ember.computed(function(){
        return {"secret": "1234"};
    })
});

And that way when I enter /filter app/routes/filter/index

import Ember from 'ember';

export default Ember.Route.extend({
        model() {
                    return this.store.findAll('filter');

        }
});

I can see that url is build ok as http://ift.tt/2fWYRJM and there is no 404 but I get error

Error while processing route: filter.index The adapter operation was aborted EmberError@http://ift.tt/2fF6VMf

and as I don't fully grasp the know-how of Ember Inspector im trying to figure this out somehow

My filter model that worked before (with mirage) looks like this:

import DS from 'ember-data';

export default DS.Model.extend({
        name: DS.attr(),
        url: DS.attr()
});

The api returns list [{"id":1, "name": "namex", "url": "http://"},{"id":2, "name": "namey", "url": "http://"}]

I'm sure If its the way api return data as its not "filters" as I had problem with plurals before.




Aucun commentaire:

Enregistrer un commentaire