diff --git a/dist/ember-json-api.js b/dist/ember-json-api.js index 42a2210..525f3ec 100644 --- a/dist/ember-json-api.js +++ b/dist/ember-json-api.js @@ -167,7 +167,7 @@ define("json-api-adapter", /** * Patch the extractSingle method, since there are no singular records */ - extractSingle: function(store, primaryType, payload, recordId, requestType) { + extractSingle: function(store, primaryType, payload, recordId, requestType, record) { var primaryTypeName; if (this.keyForAttribute) { primaryTypeName = this.keyForAttribute(primaryType.typeKey); @@ -186,7 +186,7 @@ define("json-api-adapter", json[key] = payload[key]; } } - return this._super(store, primaryType, json, recordId, requestType); + return this._super(store, primaryType, json, recordId, requestType, record); }, /** @@ -227,8 +227,7 @@ define("json-api-adapter", delete payload.links; } if (payload.linked) { - this.extractLinked(payload.linked); - delete payload.linked; + this.extractLinked(payload); } return payload; }, @@ -236,8 +235,8 @@ define("json-api-adapter", /** * Extract top-level "linked" containing associated objects */ - extractLinked: function(linked) { - var link, values, value, relation; + extractLinked: function(payload) { + var link, values, value, relation, linked = payload.linked; var store = get(this, 'store'); for (link in linked) { @@ -252,8 +251,16 @@ define("json-api-adapter", delete value.links; } } + + if (payload[link]){ + payload[link].pushObjects(linked[link]); + }else{ + payload[link] = linked[link]; + } + + delete linked[link]; } - store.pushPayload(linked); + delete payload.linked; }, /** diff --git a/dist/ember-json-api.min.js b/dist/ember-json-api.min.js index 46a59d5..d1a68bc 100644 --- a/dist/ember-json-api.min.js +++ b/dist/ember-json-api.min.js @@ -1 +1 @@ -define("json-api-adapter",["exports"],function(e){"use strict";function r(e,r,t){this.status=e,this.message=r,this.xhr=t,this.stack=(new Error).stack}var t=Ember.get;DS._routes=Ember.create(null),DS.JsonApiAdapter=DS.RESTAdapter.extend({defaultSerializer:"DS/jsonApi",buildURL:function(e,r){var i=DS._routes[e];if(i){var n=[],s=t(this,"host"),a=this.urlPrefix(),o=/\{(.*?)\}/g;return r?o.test(i)?n.push(i.replace(o,r)):n.push(i,r):n.push(i.replace(o,"")),a&&n.unshift(a),n=n.join("/"),!s&&n&&(n="/"+n),n}return this._super(e,r)},findMany:function(e,r,t){return this.ajax(this.buildURL(r.typeKey,t.join(",")),"GET")},createRecord:function(e,r,t){var i={};return i[this.pathForType(r.typeKey)]=e.serializerFor(r.typeKey).serialize(t,{includeId:!0}),this.ajax(this.buildURL(r.typeKey),"POST",{data:i})},updateRecord:function(e,r,i){var n={};n[this.pathForType(r.typeKey)]=e.serializerFor(r.typeKey).serialize(i,{includeId:!0});var s=t(i,"id");return this.ajax(this.buildURL(r.typeKey,s),"PUT",{data:n})},_tryParseErrorResponse:function(e){try{return Ember.$.parseJSON(e)}catch(r){return"Something went wrong"}},ajaxError:function(e){var t,i=this._super(e);if(e&&"object"==typeof e){t=this._tryParseErrorResponse(e.responseText);var n={};return t&&"object"==typeof t&&void 0!==t.errors&&Ember.A(Ember.keys(t.errors)).forEach(function(e){n[Ember.String.camelize(e)]=t.errors[e]}),422===e.status?new DS.InvalidError(n):new r(e.status,t,e)}return i},serializeIntoHash:function(e,r,t,i){var n=underscore(decamelize(r.typeKey));e[n]=this.serialize(t,i)},pathForType:function(e){var r=Ember.String.decamelize(e);return Ember.String.pluralize(r)}}),r.prototype=Ember.create(Error.prototype),r.constructor=r,DS.JsonApiAdapter.ServerError=r,e["default"]=DS.JsonApiAdapter}),define("json-api-serializer",["exports"],function(e){"use strict";function r(e,r,t){var i=t;return i&&e!==r&&(i={id:i,type:r}),i}function t(e,r,t,n){var s=i(t,n).mapBy("id");return s&&e!==Ember.String.pluralize(r)&&(s={ids:s,type:r}),s}var i=Ember.get,n=Ember.isNone;DS.JsonApiSerializer=DS.RESTSerializer.extend({keyForRelationship:function(e){return e},extractSingle:function(e,r,t,i,n){var s;s=this.keyForAttribute?this.keyForAttribute(r.typeKey):r.typeKey;var a={};for(var o in t){var p=Ember.String.singularize(o);p===s&&Ember.isArray(t[o])?a[p]=t[o][0]:a[o]=t[o]}return this._super(e,r,a,i,n)},normalize:function(e,r,t){var i={};for(var n in r)if("links"!==n)i[n]=r[n];else if("object"==typeof r[n])for(var s in r[n]){var a=r[n][s];a&&"object"==typeof a&&a.href?(i.links=i.links||{},i.links[s]=a.href):i[s]=a&&"object"==typeof a&&a.ids?a.ids:a}return this._super(e,i,t)},normalizePayload:function(e){return e.meta&&(this.extractMeta(e.meta),delete e.meta),e.links&&(this.extractLinks(e.links),delete e.links),e.linked&&(this.extractLinked(e.linked),delete e.linked),e},extractLinked:function(e){var r,t,n,s,a=i(this,"store");for(r in e){t=e[r];for(var o=t.length-1;o>=0;o--)if(n=t[o],n.links){for(s in n.links)n[s]=n.links[s];delete n.links}}a.pushPayload(e)},extractMeta:function(){},extractLinks:function(e){var r,t,i,n,s,a,o=[];for(r in e)t=r.split(".").pop(),i=e[r],"string"==typeof i?n=i:(t=i.type||t,n=i.href),"http"===n.substr(0,4).toLowerCase()&&(n=n.split("//").pop().split("/").slice(1).join("/")),"/"===n.charAt(0)&&(n=n.substr(1)),s={},a=Ember.String.singularize(t),s[a]=n,o.push(s),DS._routes[a]=n;return o},serializeBelongsTo:function(e,t,s){var a=s.key,o=i(e,a),p=this.keyForRelationship(s.type.typeKey),u=this.keyForRelationship(a);n(o)||(t.links=t.links||{},t.links[u]=r(u,p,i(o,"id")))},serializeHasMany:function(e,r,i){var n=i.key,s=this.keyForRelationship(i.type.typeKey),a=this.keyForRelationship(n);"hasMany"===i.kind&&(r.links=r.links||{},r.links[a]=t(a,s,e,n))}}),e["default"]=DS.JsonApiSerializer}); \ No newline at end of file +define("json-api-adapter",["exports"],function(e){"use strict";function r(e,r,t){this.status=e,this.message=r,this.xhr=t,this.stack=(new Error).stack}var t=Ember.get;DS._routes=Ember.create(null),DS.JsonApiAdapter=DS.RESTAdapter.extend({defaultSerializer:"DS/jsonApi",buildURL:function(e,r){var i=DS._routes[e];if(i){var n=[],s=t(this,"host"),a=this.urlPrefix(),o=/\{(.*?)\}/g;return r?o.test(i)?n.push(i.replace(o,r)):n.push(i,r):n.push(i.replace(o,"")),a&&n.unshift(a),n=n.join("/"),!s&&n&&(n="/"+n),n}return this._super(e,r)},findMany:function(e,r,t){return this.ajax(this.buildURL(r.typeKey,t.join(",")),"GET")},createRecord:function(e,r,t){var i={};return i[this.pathForType(r.typeKey)]=e.serializerFor(r.typeKey).serialize(t,{includeId:!0}),this.ajax(this.buildURL(r.typeKey),"POST",{data:i})},updateRecord:function(e,r,i){var n={};n[this.pathForType(r.typeKey)]=e.serializerFor(r.typeKey).serialize(i,{includeId:!0});var s=t(i,"id");return this.ajax(this.buildURL(r.typeKey,s),"PUT",{data:n})},_tryParseErrorResponse:function(e){try{return Ember.$.parseJSON(e)}catch(r){return"Something went wrong"}},ajaxError:function(e){var t,i=this._super(e);if(e&&"object"==typeof e){t=this._tryParseErrorResponse(e.responseText);var n={};return t&&"object"==typeof t&&void 0!==t.errors&&Ember.A(Ember.keys(t.errors)).forEach(function(e){n[Ember.String.camelize(e)]=t.errors[e]}),422===e.status?new DS.InvalidError(n):new r(e.status,t,e)}return i},serializeIntoHash:function(e,r,t,i){var n=underscore(decamelize(r.typeKey));e[n]=this.serialize(t,i)},pathForType:function(e){var r=Ember.String.decamelize(e);return Ember.String.pluralize(r)}}),r.prototype=Ember.create(Error.prototype),r.constructor=r,DS.JsonApiAdapter.ServerError=r,e["default"]=DS.JsonApiAdapter}),define("json-api-serializer",["exports"],function(e){"use strict";function r(e,r,t){var i=t;return i&&e!==r&&(i={id:i,type:r}),i}function t(e,r,t,n){var s=i(t,n).mapBy("id");return s&&e!==Ember.String.pluralize(r)&&(s={ids:s,type:r}),s}var i=Ember.get,n=Ember.isNone;DS.JsonApiSerializer=DS.RESTSerializer.extend({keyForRelationship:function(e){return e},extractSingle:function(e,r,t,i,n,s){var a;a=this.keyForAttribute?this.keyForAttribute(r.typeKey):r.typeKey;var o={};for(var p in t){var u=Ember.String.singularize(p);u===a&&Ember.isArray(t[p])?o[u]=t[p][0]:o[p]=t[p]}return this._super(e,r,o,i,n,s)},normalize:function(e,r,t){var i={};for(var n in r)if("links"!==n)i[n]=r[n];else if("object"==typeof r[n])for(var s in r[n]){var a=r[n][s];a&&"object"==typeof a&&a.href?(i.links=i.links||{},i.links[s]=a.href):i[s]=a&&"object"==typeof a&&a.ids?a.ids:a}return this._super(e,i,t)},normalizePayload:function(e){return e.meta&&(this.extractMeta(e.meta),delete e.meta),e.links&&(this.extractLinks(e.links),delete e.links),e.linked&&this.extractLinked(e),e},extractLinked:function(e){{var r,t,n,s,a=e.linked;i(this,"store")}for(r in a){t=a[r];for(var o=t.length-1;o>=0;o--)if(n=t[o],n.links){for(s in n.links)n[s]=n.links[s];delete n.links}e[r]?e[r].pushObjects(a[r]):e[r]=a[r],delete a[r]}delete e.linked},extractMeta:function(){},extractLinks:function(e){var r,t,i,n,s,a,o=[];for(r in e)t=r.split(".").pop(),i=e[r],"string"==typeof i?n=i:(t=i.type||t,n=i.href),"http"===n.substr(0,4).toLowerCase()&&(n=n.split("//").pop().split("/").slice(1).join("/")),"/"===n.charAt(0)&&(n=n.substr(1)),s={},a=Ember.String.singularize(t),s[a]=n,o.push(s),DS._routes[a]=n;return o},serializeBelongsTo:function(e,t,s){var a=s.key,o=i(e,a),p=this.keyForRelationship(s.type.typeKey),u=this.keyForRelationship(a);n(o)||(t.links=t.links||{},t.links[u]=r(u,p,i(o,"id")))},serializeHasMany:function(e,r,i){var n=i.key,s=this.keyForRelationship(i.type.typeKey),a=this.keyForRelationship(n);"hasMany"===i.kind&&(r.links=r.links||{},r.links[a]=t(a,s,e,n))}}),e["default"]=DS.JsonApiSerializer}); \ No newline at end of file