Fix loading record by slug

In a repo route we need to find record by slug there is no easy way to
do it with a public finders API, so we need to use adapter and
serializers directly. The problem is that the old way of doing this
didn't use the normalizePayload function and also it didn't add included
records properly. New code properly normalizes response and adds all of
the embedded records that were extracted from the response.
This commit is contained in:
Piotr Sarnacki 2015-11-18 17:01:59 +01:00
parent 7ae4d6aa7d
commit c3fd0d8e98
2 changed files with 19 additions and 3 deletions

View File

@ -162,8 +162,16 @@ Repo.reopenClass
else
adapter = store.adapterFor('repo')
modelClass = store.modelFor('repo')
adapter.findRecord(store, modelClass, slug).then (resourceHash) ->
store.push(store.normalize('repo', resourceHash));
adapter.findRecord(store, modelClass, slug).then (payload) ->
serializer = store.serializerFor('repo')
modelClass = store.modelFor('repo')
result = serializer.normalizeResponse(store, modelClass, payload, null, 'findRecord')
repo = store.push(data: result.data)
for record in result.included
r = store.push(data: record)
repo
, ->
error = new Error('repo not found')
error.slug = slug

View File

@ -2,7 +2,15 @@ import Ember from 'ember';
import V2FallbackSerializer from 'travis/serializers/v2_fallback';
var Serializer = V2FallbackSerializer.extend({
isNewSerializerAPI: true
isNewSerializerAPI: true,
normalizeResponse(store, primaryModelClass, payload, id, requestType) {
if(!id && requestType == 'findRecord') {
id = payload.id;
}
return this._super(store, primaryModelClass, payload, id, requestType);
}
});
export default Serializer;