diff --git a/bower.json b/bower.json index b0c80a7..c847033 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "ember-json-api", - "version": "0.0.13", + "version": "0.0.14", "homepage": "https://github.com/plyfe/ember-json-api", "authors": [ "Dali Zheng ", @@ -15,7 +15,7 @@ "tests" ], "dependencies": { - "ember-data": "https://s3.amazonaws.com/asset-staging-playground/ember-data-1.0.0-patch-beta.10.tar.gz", + "ember-data": "https://s3.amazonaws.com/asset-staging-playground/ember-data-1.0.0-patch2-beta.10.tar.gz", "ember": "v1.7.0" }, "devDependencies": { diff --git a/dist/ember-json-api.js b/dist/ember-json-api.js index 3bca043..352219d 100644 --- a/dist/ember-json-api.js +++ b/dist/ember-json-api.js @@ -168,7 +168,7 @@ /** * 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); @@ -187,7 +187,7 @@ json[key] = payload[key]; } } - return this._super(store, primaryType, json, recordId, requestType); + return this._super(store, primaryType, json, recordId, requestType, record); }, /** @@ -220,8 +220,7 @@ delete payload.links; } if (payload.linked) { - this.extractLinked(payload.linked); - delete payload.linked; + this.extractLinked(payload); } return payload; }, @@ -229,8 +228,8 @@ /** * 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) { @@ -245,8 +244,16 @@ 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 1e28523..84f401a 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)];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)}}),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";var r=Ember.get,t=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 u=Ember.String.singularize(o);u===s&&Ember.isArray(t[o])?a[u]=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])i[s]=r[n][s];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 t,i,n,s,a=r(this,"store");for(t in e){i=e[t];for(var o=i.length-1;o>=0;o--)if(n=i[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,i,n){var s=n.key,a=r(e,s),o=this.keyForRelationship(s);t(a)||(i.links=i.links||{},i.links[o]=r(a,"id"))},serializeHasMany:function(e,t,i){var n=i.key,s=this.keyForRelationship(n);"hasMany"===i.kind&&(t.links=t.links||{},t.links[s]=r(e,n).mapBy("id"))}}),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)];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)}}),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";var r=Ember.get,t=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 u in t){var l=Ember.String.singularize(u);l===a&&Ember.isArray(t[u])?o[l]=t[u][0]:o[u]=t[u]}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])i[s]=r[n][s];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 t,i,n,s,a=e.linked;r(this,"store")}for(t in a){i=a[t];for(var o=i.length-1;o>=0;o--)if(n=i[o],n.links){for(s in n.links)n[s]=n.links[s];delete n.links}e[t]?e[t].pushObjects(a[t]):e[t]=a[t],delete a[t]}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,i,n){var s=n.key,a=r(e,s),o=this.keyForRelationship(s);t(a)||(i.links=i.links||{},i.links[o]=r(a,"id"))},serializeHasMany:function(e,t,i){var n=i.key,s=this.keyForRelationship(n);"hasMany"===i.kind&&(t.links=t.links||{},t.links[s]=r(e,n).mapBy("id"))}}),e["default"]=DS.JsonApiSerializer}); \ No newline at end of file