3
votes

I'm using Ember Data (1.0.0-beta.7) with DS.RESTAdapter and a spanish API. I have a model call App.ModuloPerfil that represents a ternary many to many relationship:

App.Perfil = DS.Model.extend({
  nombre: DS.attr(),
  tipo: DS.attr(),
  modulosPerfiles: DS.hasMany('moduloPerfil', { async: true })
});

App.Modulo = DS.Model.extend({
  nombre: DS.attr(),
  alias: DS.attr(),
  tipo: DS.attr(),
  modulosPerfiles: DS.hasMany('moduloPerfil', { async: true })
});

App.ModuloPerfil = DS.Model.extend({
  enabled: DS.attr(),
  perfil: DS.belongsTo('perfil', { async: true }),
  modulo: DS.belongsTo('modulo', { async: true })
});

I have defined some inflector rules to deal with the spanish plurals:

Ember.Inflector.inflector.irregular('perfil', 'perfiles');
Ember.Inflector.inflector.irregular('moduloPerfil', 'modulosPerfiles');

When I make an API request to: /api/perfil/1 I get the following response:

{"perfil": {id: 1, nombre: "Perfil investigador", tipo: "Investigador", modulosPerfiles: [1,2]}}

It seems it's all ok so far, but when I check the calls made to the API from the client I see that when it tries to retrieve de moduloPerfil element the url is:

GET /api/moduloPerfils?ids[]=1&ids[]=2

It uses "moduloPerfils", the english plural and not the irregular rule defined in the inflector.

I have tried with Ember.Inflector.inflector.irregular('modulo-perfil', 'modulos-perfiles') but I doesn't work neither.

How the irregular rule must be defined?

1

1 Answers

2
votes

After some more testing I have discovered the error. It's not a rules problem, the problem was that the Ember.Inflector rules must be declared before Ember.Application.create

I was doing:

var App = Ember.Application.create();

Ember.Inflector.inflector.irregular('perfil', 'perfiles');
Ember.Inflector.inflector.irregular('moduloPerfil', 'modulosPerfiles');

And the correct order is:

Ember.Inflector.inflector.irregular('perfil', 'perfiles');
Ember.Inflector.inflector.irregular('moduloPerfil', 'modulosPerfiles');

var App = Ember.Application.create();

Sorry, it's a silly mistake.

FIXED: Finally this is not an error and the code it's right. The rules can be declared before or after the App creation. The original code works without errors so the problem maybe was another thing that I didn't see.