Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

findAll issue when URL has a namespace #289

Closed
akshayrawat opened this Issue Jun 14, 2012 · 7 comments

Comments

Projects
None yet
3 participants

Based on the discussion here

If a resource is being served under a namespace, eg

App.Event = DS.Model.extend({
  name: DS.attr('string'),
});

App.Event.reopenClass({
  url: 'api/event'
})

When ember-data parses the json response, it does something like json[plural] in findAll which should be json['events'], however the plural is calculated to be json['api/events'], and hence the error Uncaught TypeError: Cannot call method 'map' of undefined

This is reproducible on ember-rails 0.6.0

Member

bradleypriest commented Jun 16, 2012

You can define a plurals object when you create your store to handle things like this:

App.store = DS.Store.create({
  revision: 4,
  adapter: DS.RESTAdapter.create({
    plurals: {
      'api/event': 'events'
    }
  })
});

However if your entire app's endpoints are namespaced you can provide a namespace option:

App.store = DS.Store.create({
  revision: 4,
  adapter: DS.RESTAdapter.create({
    namespace: 'api'
  })
});

Thanks for your reply. My use case is that some resources use a api namespace, and some resources use a admin namespace. Thats why I had to specify the namespace in the url of the DS.Model. Not sure if the url parameter was mean't to have a namespace like that in the first place ?

Member

bradleypriest commented Jun 16, 2012

In that case the first case should suit you, or am I missing something?

Member

tchak commented Jun 16, 2012

Given current design, url property is not mean't to have a namespace
If you really need different namespaces for different urls, plurals is a way to go.

Member

tchak commented Jun 16, 2012

Actually, you really should not set url property with a namespace.

  rootForType: function(type) {
    if (type.url) { return type.url; }

    // use the last part of the name as the URL
    var parts = type.toString().split(".");
    var name = parts[parts.length - 1];
    return name.replace(/([A-Z])/g, '_$1').toLowerCase().slice(1);
  }

It will not work in rootForType method.
You should do override buildURL method and do your url building manually.
RESTAdapter use the plural form for url AND for root of json.
Maybe we should have root property on the model.

Thanks guys. You may close this issue if you want to.

Member

tchak commented Jun 16, 2012

Could you close it? I will make a new issue (and probably a pr) for root property

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment