Failed to import file from ember-cli created utils folder

I created a utility with ember generate util

and I am trying to import it into another file, but I am getting a build error.

My utility ( app/utils/swapi-adapter.js

):

import Ember from 'ember';

/* global $ */
export default Ember.Object.extend({
  base: 'http://swapi.co/api/',

  get: function(uri) {
    var url = uri;

    // make sure to append url
    if(url.indexOf(this.get('base')) !== -1) {
       url = this.get('base') + url;
    }

    return $.getJSON(url);
  }
});

      

The route I'm trying to import it from is: ( app/routes/test-api.js

)

import Ember from 'ember';
import SWAPI from 'utils/swapi-adapter';

export default Ember.Route.extend({
  model: {},
  actions: {
    test: function(url) {
      SWAPI.get(url);
    }
  }
});

      

+3


source to share


2 answers


I think the "correct" way to import the utility is actually:

import SWAPI from '[app-name]/utils/swapi-adapter';

      



Although in the case of an adapter, this is probably no better.

+4


source


You need a relative path, since the adapter is in a different directory than the route:

import SWAPI from '../utils/swapi-adapter';

      



You can use dependency injection to give all of your routes access to the adapter.

+2


source







All Articles