Search code examples
ember.jsember-dataember-cli

ember cli naming conventions for models and relative adapters


is it possible to have model names like geo-data? Let me explain

I have a model like this

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

var geoData = DS.Model.extend({
    name: DS.attr('string')
});

geoData.reopenClass({
    FIXTURES: [
        {
            name: 'foo'
        }
    ]
});

export default geoData;

then I've a route

// app/routes/index.js
import Ember from 'ember';

export default Ember.Route.extend({
   model: function() {
       return this.store.find('geo-data');
   }
});

and an adapter

// app/adapters/geo-data.js
import DS from 'ember-data';

export default DS.FixtureAdapter.extend({});

but my app keeps trying to send a GET request to http://customhost.com/geoData

  1. First it shouldn't perform any request, it's under a fixture adapter
  2. It sends a request to /geoData not /geo-data

I'm missing something for sure, could you please enlight me? Thank you


Solution

  • The REST Adapter by default will camelize and pluralize your models to get the associated endpoint. If you would like to override this functionality you can override the adapter. See http://emberjs.com/api/data/classes/DS.RESTAdapter.html#method_pathForType for all documentation, but for your scenario if you would like to keep the dashes you can do something like

    export default DS.RESTAdapter.extend({
      pathForType: function(type) {
        var dasherized = Ember.String.dasherize(type);
        return dasherized;
      }
    });
    

    As far as the issue with the fixture adapter sending out requests, that seems very strange, and it shouldn't be. I did notice that your fixture data does not have an id and it definitely should. Here is an example using the fixture adapter.

    http://emberjs.jsbin.com/firore/1/edit?html,css,js,output

    App = Ember.Application.create();
    
    App.Router.map(function() {
      // put your routes here
    });
    
    App.IndexRoute = Ember.Route.extend({
      model: function() {
        return this.store.find('foo');
      }
    });
    
    
    App.FooAdapter = DS.FixtureAdapter.extend({});
    App.FooModel = DS.Model.extend({
      name: DS.attr()
    });
    
    App.FooModel.reopenClass({
        FIXTURES: [
            {
                id: 1,
                name: 'foo'
            },
            {
                id:2,
                name: 'bar'
            }
        ]
    });
    

    I would also note, that I've had issues in the past with objects named 'data' that can cause unexpected behavior.