Skip to content

Commit

Permalink
Rebase and bring up to date with JSON-API RC3
Browse files Browse the repository at this point in the history
  • Loading branch information
wecc committed Apr 22, 2015
1 parent 0955b53 commit da54b6e
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 139 deletions.
50 changes: 23 additions & 27 deletions packages/ember-data/lib/adapters/json-api-adapter.js
Expand Up @@ -33,48 +33,48 @@ export default Adapter.extend(BuildURLMixin, {
@type {String}
*/

find: function(store, type, id, record) {
return this.ajax(this.buildURL(type.typeKey, id, record), 'GET');
find: function(store, type, id, snapshot) {
return this.ajax(this.buildURL(type.typeKey, id, snapshot, 'find'), 'GET');
},

findAll: function(store, type) {
return this.ajax(this.buildURL(type.typeKey), 'GET');
return this.ajax(this.buildURL(type.typeKey, null, null, 'findAll'), 'GET');
},

findMany: function(store, type, ids, records) {
findMany: function(store, type, ids, snapshots) {
var data = {
filter: {
id: ids.join(',')
}
};
return this.ajax(this.buildURL(type.typeKey, ids, records), 'GET', { data: data });
return this.ajax(this.buildURL(type.typeKey, ids, snapshots, 'findMany'), 'GET', { data: data });
},

findQuery: function(store, type, query) {
return this.ajax(this.buildURL(type.typeKey), 'GET', { data: query });
return this.ajax(this.buildURL(type.typeKey, query, null, 'findQuery'), 'GET', { data: query });
},



findBelongsTo: function(store, record, link, relationship) {
findBelongsTo: function(store, snapshot, link, relationship) {
var url, id, type;

if (link.resource) {
url = link.resource;
if (link.related) {
url = link.related;
} else {
id = get(record, 'id');
type = record.constructor.typeKey;
url = this.buildURL(type, id);
id = snapshot.id;
type = snapshot.typeKey;
url = this.buildURL(type, id, null, 'findBelongsTo');
}

return this.ajax(url, 'GET');
},

findHasMany: function(store, record, link, relationship) {
findHasMany: function(store, snapshot, link, relationship) {
var url; //, id, type, host;

if (link.resource) {
url = link.resource;
if (link.related) {
url = link.related;
} else {
// TODO
return;
Expand All @@ -85,30 +85,26 @@ export default Adapter.extend(BuildURLMixin, {



createRecord: function(store, type, record) {
createRecord: function(store, type, snapshot) {
var serializer = store.serializerFor(type.typeKey);

var snapshot = record._createSnapshot();
var data = serializer.serialize(snapshot);

return this.ajax(this.buildURL(type.typeKey, null, record), "POST", { data: data });
return this.ajax(this.buildURL(type.typeKey, null, snapshot, 'createRecord'), 'POST', { data: data });
},

updateRecord: function(store, type, record) {
updateRecord: function(store, type, snapshot) {
var serializer = store.serializerFor(type.typeKey);

var snapshot = record._createSnapshot();
var data = serializer.serialize(snapshot, { includeId: true });

var id = get(record, 'id');
var id = snapshot.id;

return this.ajax(this.buildURL(type.typeKey, id, record), "PUT", { data: data });
return this.ajax(this.buildURL(type.typeKey, id, snapshot, 'updateRecord'), 'PATCH', { data: data });
},

deleteRecord: function(store, type, record) {
var id = get(record, 'id');
deleteRecord: function(store, type, snapshot) {
var id = snapshot.id;

return this.ajax(this.buildURL(type.typeKey, id, record), "DELETE");
return this.ajax(this.buildURL(type.typeKey, id, snapshot, 'deleteRecord'), 'DELETE');
},


Expand Down
127 changes: 61 additions & 66 deletions packages/ember-data/lib/serializers/json-api-serializer.js
Expand Up @@ -39,11 +39,12 @@ export default Serializer.extend({

var dataType = Ember.typeOf(payload.data);

if (dataType === 'object') {
return this.extractSingle(store, payload, id);
} else if (dataType === 'array') {
store.setMetadataFor(type, payload.meta || {});
return this.extractArray(store, payload);
switch (dataType) {
case 'object':
return this.extractSingle(store, payload, id);
case 'array':
store.setMetadataFor(type, payload.meta || {});
return this.extractArray(store, payload);
}
},

Expand Down Expand Up @@ -121,10 +122,13 @@ export default Serializer.extend({
}, this);

snapshot.eachRelationship(function(key, relationship) {
if (relationship.kind === 'belongsTo') {
this.serializeBelongsTo(snapshot, json, relationship);
} else if (relationship.kind === 'hasMany') {
this.serializeHasMany(snapshot, json, relationship);
switch (relationship.kind) {
case 'belongsTo':
this.serializeBelongsTo(snapshot, json, relationship);
break;
case 'hasMany':
this.serializeHasMany(snapshot, json, relationship);
break;
}
}, this);

Expand Down Expand Up @@ -153,15 +157,16 @@ export default Serializer.extend({
var links = json['links'] = json['links'] || {};

var payloadKey = this.keyForRelationship(key, 'belongsTo');
var linkage = null;

if (Ember.isNone(belongsTo)) {
links[payloadKey] = { id: null };
} else {
links[payloadKey] = {
if (!Ember.isNone(belongsTo)) {
linkage = {
type: this.serializeTypeKey(belongsTo.typeKey),
id: belongsTo.id
};
}

links[payloadKey] = { linkage: linkage };
},

serializeHasMany: function(snapshot, json, relationship) {
Expand All @@ -171,23 +176,16 @@ export default Serializer.extend({
var links = json['links'] = json['links'] || {};

var payloadKey = this.keyForRelationship(key, 'hasMany');
var linkage = [];

if (hasMany.length === 0) {
links[payloadKey] = { ids: [] };
} else {

// TODO: if all items in hasMany is of the same type, provide
// { type: "type", ids: [..] } instead of { data: [...] }

var data = [];
for (var i = 0; i < hasMany.length; i++) {
data.push({
type: this.serializeTypeKey(hasMany[i].typeKey),
id: hasMany[i].id
});
}
links[payloadKey] = { data: data };
for (var i = 0; i < hasMany.length; i++) {
linkage.push({
type: this.serializeTypeKey(hasMany[i].typeKey),
id: hasMany[i].id
});
}

links[payloadKey] = { linkage: linkage };
},


Expand All @@ -197,7 +195,6 @@ export default Serializer.extend({

this.normalizeAttributes(type, hash);
this.normalizeRelationships(type, hash);
this.normalizeLinks(hash);
this.applyTransforms(type, hash);

return hash;
Expand Down Expand Up @@ -227,60 +224,58 @@ export default Serializer.extend({

if (hash.links[payloadKey]) {
link = hash.links[payloadKey];

if (relationship.kind === 'belongsTo' && link.id) {

hash[key] = { id: link.id, type: this.normalizeTypeKey(link.type) };
delete hash.links[payloadKey];

} else if (relationship.kind === 'hasMany') {

if (link.ids) {

hash[key] = map.call(link.ids, function(id) {
return { id: id, type: this.normalizeTypeKey(link.type) };
}, this);
delete hash.links[payloadKey];

} else if (link.data) {

hash[key] = map.call(link.data, function(item) {
return { id: item.id, type: this.normalizeTypeKey(item.type) };
}, this);
delete hash.links[payloadKey];

delete hash.links[payloadKey];

if (link.linkage) {
switch (relationship.kind) {
case 'belongsTo':
this.normalizeBelongsTo(hash, key, link);
break;
case 'hasMany':
this.normalizeHasMany(hash, key, link);
break;
}
} else {
this.normalizeLink(hash, key, link);
}
}
}, this);
},

normalizeLinks: function(hash) {
var links = hash.links;

if (!links) { return; }

for (var key in links) {
links[key] = this.normalizeLink(links[key]);
normalizeBelongsTo: function(hash, key, link) {
var linkage = link.linkage;
if (linkage) {
hash[key] = {
type: this.normalizeTypeKey(linkage.type),
id: linkage.id
};
}
},

normalizeLink: function(link) {
var normalizedLink = {};
normalizeHasMany: function(hash, key, link) {
var linkage = link.linkage;
if (linkage) {
hash[key] = map.call(linkage, function(item) {
return {
type: this.normalizeTypeKey(item.type),
id: item.id
};
}, this);
}
},

normalizeLink: function(hash, key, link) {
if (Ember.typeOf(link) === 'string') {
normalizedLink = {
hash.links[key] = {
self: null,
resource: link
related: link
};
} else {
normalizedLink = {
hash.links[key] = {
self: link.self || null,
resource: link.resource || null
related: link.related || null
};
}

return normalizedLink;
},

applyTransforms: function(type, hash) {
Expand Down

0 comments on commit da54b6e

Please sign in to comment.