13

I am trying to get my ember model to use a remote rails api. I've tried extending DS.RESTAdapter but I don't know how to tell the model to use those settings. My model never reaches out to localhost:3000.

    //app.js
    import Ember from 'ember';
    import Resolver from 'ember/resolver';
    import loadInitializers from 'ember/load-initializers';

    Ember.MODEL_FACTORY_INJECTIONS = true;

    var App = Ember.Application.extend({
      modulePrefix: 'friend-ember-app', // TODO: loaded via config
      Resolver: Resolver
    });
    App.ApplicationAdapter = DS.RESTAdapter.extend({
        host: 'localhost:3000',
        namespace: 'api/v1'
    });

    loadInitializers(App, 'friend-ember-app');

    export default App;

//

    //models/event.js
    import DS from 'ember-data';

    export default DS.Model.extend({
        title: DS.attr('string'),
        description: DS.attr('string')
    });

//

    // controllers/index.js 
    import Ember from 'ember';

    export default Ember.Controller.extend({
        columns: function(){
            //Attempting to see request to remote api
            var events = this.store.all('event');
            debugger;
            return [1,2,3,4];
        }.property()
    });
MikeV
  • 657
  • 1
  • 8
  • 20

1 Answers1

21

The adapter needs to be defined and exported in a separate file in adapters folder

In adapters/application.js

import DS from "ember-data";

var ApplicationAdapter = DS.RESTAdapter.extend({
    host: 'localhost:3000',
    namespace: 'api/v1'
});

export default ApplicationAdapter;
ViRa
  • 714
  • 5
  • 7
  • Thanks ViRa that fixed it. Follow up question: Why was I not able to set the RESTAdapter via App.ApplicationAdapter in my app.js? Maybe it's my lack of understanding the namespaces of ember. Any info would be appreciated. – MikeV Jul 22 '14 at 13:00
  • 2
    From what I understand each javascript file is mapped to a es6 javascript module and only the exported functions and objects are accessible to other modules. In your case, the applicationadapter was not exported. Number two, the naming of file and the folders have some meaning in ember which i don't get completely. All adapter related codes sit in adapters folder. Similar to how controllers, models, etc have their own folder. (Sorry if I am not of much help. I am quite new to ember as well.) – ViRa Jul 22 '14 at 14:14
  • 1
    I have a similar problem trying to extend the Application adapter as explained [here](http://andycrum.com/2014/06/02/getting-started-with-ember-data/) at pragraph *An aside: specifying a different host and namespace*. I can't understand how I can refer to `ApplicationAdapter` in ember-cli. – masciugo Nov 04 '14 at 16:57
  • @masciugo did you ever figure it out? – Chris Watts Feb 13 '15 at 06:22
  • `import ApplicationAdapter from './application'; export default ApplicationAdapter.extend({ });` – Gabe Rainbow Mar 24 '15 at 04:37
  • Is there documentation anywhere that explains where to do this and why it is done this way? – Kurt Mueller May 11 '15 at 23:51
  • Found it: http://www.ember-cli.com/#adapters--serializers. --- Oops, this is for having adapters on a per-type basis. – Kurt Mueller May 11 '15 at 23:52