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 atmy.api.url/persons/1
- the
human_identity
model atmy.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 returnundefined
. - 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
?
Thanks.