Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement extractMeta for new Serializer API #3218

Merged
merged 1 commit into from
Jun 6, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion packages/ember-data/lib/serializers/json-serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,11 @@ export default Serializer.extend({
included: []
};

payload = this.normalizePayload(payload);
let meta = this.extractMeta(store, primaryModelClass, payload);
if (meta) {
Ember.assert('The `meta` returned from `extractMeta` has to be an object, not "' + Ember.typeOf(meta) + '".', Ember.typeOf(meta) === 'object');
documentHash.meta = meta;
}

if (isSingle) {
let { data } = this.normalize(primaryModelClass, payload);
Expand Down Expand Up @@ -1443,6 +1447,10 @@ export default Serializer.extend({
@param {Object} payload
*/
extractMeta: function(store, typeClass, payload) {
if (Ember.FEATURES.isEnabled('ds-new-serializer-api') && this.get('isNewSerializerAPI')) {
return _newExtractMeta.apply(this, arguments);
}

if (payload && payload.meta) {
store.setMetadataFor(typeClass, payload.meta);
delete payload.meta;
Expand Down Expand Up @@ -1592,3 +1600,19 @@ function _newNormalize(modelClass, resourceHash) {

return { data };
}

/*
@method _newExtractMeta
@param {DS.Store} store
@param {DS.Model} modelClass
@param {Object} payload
@return {Object}
@private
*/
function _newExtractMeta(store, modelClass, payload) {
if (payload && payload.hasOwnProperty('meta')) {
let meta = payload.meta;
delete payload.meta;
return meta;
}
}
26 changes: 16 additions & 10 deletions packages/ember-data/lib/serializers/rest-serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,17 @@ var RESTSerializer = JSONSerializer.extend({
@private
*/
_normalizeResponse: function(store, primaryModelClass, payload, id, requestType, isSingle) {
var document = {
let documentHash = {
data: null,
included: []
};

let meta = this.extractMeta(store, primaryModelClass, payload);
if (meta) {
Ember.assert('The `meta` returned from `extractMeta` has to be an object, not "' + Ember.typeOf(meta) + '".', Ember.typeOf(meta) === 'object');
documentHash.meta = meta;
}

Ember.keys(payload).forEach((prop) => {
var modelName = prop;
var forcedSecondary = false;
Expand Down Expand Up @@ -296,14 +302,14 @@ var RESTSerializer = JSONSerializer.extend({
*/
if (isPrimary && Ember.typeOf(value) !== 'array') {
let { data, included } = this.normalize(primaryModelClass, value, prop);
document.data = data;
document.included.push(...included);
documentHash.data = data;
documentHash.included.push(...included);
return;
}

let { data, included } = this.normalizeArray(store, typeName, value, prop);

document.included.push(...included);
documentHash.included.push(...included);

if (isSingle) {
/*jshint loopfunc:true*/
Expand All @@ -319,24 +325,24 @@ var RESTSerializer = JSONSerializer.extend({
in the array
*/
var isUpdatedRecord = isPrimary && coerceId(resource.id) === id;
var isFirstCreatedRecord = isPrimary && !id && !document.data;
var isFirstCreatedRecord = isPrimary && !id && !documentHash.data;

if (isFirstCreatedRecord || isUpdatedRecord) {
document.data = resource;
documentHash.data = resource;
} else {
document.included.push(resource);
documentHash.included.push(resource);
}
});
} else {
if (isPrimary) {
document.data = data;
documentHash.data = data;
} else {
document.included.push(...data);
documentHash.included.push(...data);
}
}
});

return document;
return documentHash;
},

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,29 @@ if (Ember.FEATURES.isEnabled('ds-new-serializer-api')) {
deepEqual(post.data.relationships.comments.data, [{ id: "1", type: "comment" }, { id: "2", type: "comment" }]);
});

test('normalizeSingleResponse should extract meta using extractMeta', function() {
env.registry.register("serializer:post", TestSerializer.extend({
extractMeta: function(store, modelClass, payload) {
let meta = this._super(...arguments);
meta.authors.push('Tomhuda');
return meta;
}
}));

var jsonHash = {
id: "1",
title_payload_key: "Rails is omakase",
my_comments: [1, 2],
meta: {
authors: ['Tomster']
}
};

var post = env.container.lookup("serializer:post").normalizeSingleResponse(env.store, Post, jsonHash, '1', 'find');

deepEqual(post.meta.authors, ['Tomster', 'Tomhuda']);
});

test("Serializer should respect the primaryKey attribute when extracting records", function() {
env.registry.register('serializer:post', TestSerializer.extend({
primaryKey: '_ID_'
Expand Down Expand Up @@ -120,32 +143,6 @@ if (Ember.FEATURES.isEnabled('ds-new-serializer-api')) {
deepEqual(post.data.relationships.comments.data, [{ id: "1", type: "comment" }]);
});

test("normalizePayload is called during normalizeSingleResponse", function() {
var counter = 0;

env.registry.register('serializer:post', TestSerializer.extend({
normalizePayload: function(payload) {
counter++;
return payload.response;
}
}));

var jsonHash = {
response: {
id: 1,
title: "Rails is omakase"
}
};

run(function() {
post = env.container.lookup("serializer:post").normalizeSingleResponse(env.store, Post, jsonHash, '1', 'find');
});

equal(counter, 1);
equal(post.data.id, "1");
equal(post.data.attributes.title, "Rails is omakase");
});

test("Calling normalize should normalize the payload (only the passed keys)", function () {
expect(1);
var Person = DS.Model.extend({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,25 @@ if (Ember.FEATURES.isEnabled('ds-new-serializer-api')) {
});
});

test('normalizeArrayResponse should extract meta using extractMeta', function() {
env.registry.register("serializer:home-planet", TestSerializer.extend({
extractMeta: function(store, modelClass, payload) {
let meta = this._super(...arguments);
meta.authors.push('Tomhuda');
return meta;
}
}));

var jsonHash = {
meta: { authors: ['Tomster'] },
home_planets: [{ id: "1", name: "Umber", superVillains: [1] }]
};

var json = env.container.lookup("serializer:home-planet").normalizeArrayResponse(env.store, HomePlanet, jsonHash, null, 'findAll');

deepEqual(json.meta.authors, ['Tomster', 'Tomhuda']);
});

test("normalizeArrayResponse warning with custom modelNameFromPayloadKey", function() {
var homePlanets;
env.restNewSerializer.modelNameFromPayloadKey = function(root) {
Expand Down