Override serializer.extract()?


#1

Hello,

I’m using Ember App Kit and Ember Data.
I ran bower install this morning so I’ve got the latest versions :

Ember      : 1.4.0-beta.2 
Ember Data : 1.0.0-beta.5
Handlebars : 1.3.0 
jQuery     : 2.0.3 

I have quite an abstract data model cooked by a postgresql guru.

// app/models/human.js
export default DS.Model.extend({
  creation: DS.attr(),
  modification: DS.attr(),
  sex: DS.attr(),
  person: DS.belongsTo('person'),
  human_identitys: DS.hasMany('human_identity', {async: true})
});

// app/models/person.js
export default DS.Model.extend({
  creation: DS.attr(),
  modification: DS.attr(),
  life: DS.attr(),
  address_persons: DS.hasMany('address_person')
});

// app/models/human_identity.js
export default DS.Model.extend({
  firstname: DS.attr(),
  middlename: DS.attr(),
  familyname: DS.attr(),
  human: DS.belongsTo('human'),
});

I don’t really understand what the point of the pluralize / camelize thing is, but I got it working somehow.

// app/adapters/application.js
var rules = {
  plurals: [ [/$/, 's'] ],
  singular: [ [/s$/i, ''] ]
};
var inflector = new Ember.Inflector(rules);

var myAdapter = DS.RESTAdapter.extend({
  host: 'my.api.url'
});

myAdapter.reopen({
  pathForType: function(type) {
    // no camelization
    return inflector.pluralize(type);
  }
});

export default myAdapter;

So when I enter the route human in my app, Ember Data fetches the human model on the API at my.api.url/humans/1, which returns :

{
  "human": {
    "id":"1",
    "sex":"H",
    "creation":"2011-07-14 12:12:33+02",
    "modification":"2014-01-15 08:54:23.201415+01",
    "human_identitys":["9"],
    "person":"1"
  }
}

From there, Ember Data tries to fetch

  • the person model at my.api.url/persons/1
  • the human_identity model at my.api.url/human_identitys/ids=[9]

The data is actually loaded, but I get an Assertion failed:

Assertion Failed: The response from a findMany must be an Array, not undefined 

With Chrome Debugger, I located the assertion into a _findHasMany function.

I set a breakpoint on this line:

  payload = serializer.extract(store, type, payload, null, 'findMany');

that is, when the promise resolves, so as to inspect payload (the ‘response from a findMany’).

At first, payload is a promise with an array human_identity containing the correct data from the server. But after the serializer.extract it is undefined, which causes the Assertion fail I also noticed that type.typeKey = human-identity.

Now here are my questions :

  • Is this what’s making adapter.serializer undefine the payload ?
    indeed, payload.get(type.typeKey) would return undefined.
  • So I guess I should work on typeKey so as to replace dashes with underscores. Which function should I override ?
  • What if I wanted to turn off all these Ember Data features so the payload isn’t transformed at all ?
  • Am I totally misguided :smile: ?

Thanks.


#2

This is currently an issue in Ember Data and there is a pull request to fix this https://github.com/emberjs/data/pull/1662. The issue is that typeKey was assumed to be camelized, but that’s not longer the case.

While the pull requests gets into master I have been working with my own build of ED https://gist.github.com/abuiles/8472622 try it and let me know if it fixes your issue.


#3

You can with your own serializer.


#4

https://github.com/emberjs/data/pull/1662 is now on master.


#5

Thanks @abuiles and @stefan for your answers.

I checked the PR from @abuiles : it seems I was pretty close, except I focused on myAdapter.pathForType().

I tried making myAdapter.pathForType() look like your DS.ActiveModelAdapter.pathForType() :

Adapter.reopen({
  pathForType: function(type) {
    var decamelized = Ember.String.decamelize(type);
    var underscored = Ember.String.underscore(decamelized);
    return inflector.pluralize(underscored);
  }
});

I guess I must also tweak ActiveModelAdapter. Can I just reopen DS.ActiveModelAdapter ?
(edit: tried, didn’t work, still wondering why…)

@stefan says the PR has made its way into master : I tried uninstalling and reinstalling Ember Data (with bower), but it didn’t work, I guess I’ll force the github url.


#6

OK, it’s working with @abuiles’s build. Thanks a lot !
I’ll be working with the Gist until Ember Data master makes it to bower repository.