Skip to content

Commit

Permalink
Merge pull request #185 from broerse/shouldSerializeHasMany
Browse files Browse the repository at this point in the history
Fix _shouldSerializeHasMany deprecation
  • Loading branch information
broerse authored Jun 22, 2017
2 parents b88e391 + f63f15a commit c0424b6
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions addon/serializers/pouch.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ const {
const keys = Object.keys || Ember.keys;
const assign = Object.assign || Ember.assign;

export default DS.RESTSerializer.extend({
var Serializer = DS.RESTSerializer.extend({

init: function() {
this._super(...arguments);

let config = getOwner(this).resolveRegistration('config:environment');
this.dontsavedefault = config['emberpouch'] && config['emberpouch']['dontsavehasmany'];
},

_getDontsave(relationship) {
return !Ember.isEmpty(relationship.options.dontsave) ? relationship.options.dontsave : this.dontsavedefault;
},

_shouldSerializeHasMany: function(snapshot, key, relationship) {
shouldSerializeHasMany: function(snapshot, key, relationship) {
let dontsave = this._getDontsave(relationship);
let result = !dontsave;
return result;
Expand All @@ -32,9 +32,9 @@ export default DS.RESTSerializer.extend({
serializeHasMany(snapshot, json, relationship) {
if (this._shouldSerializeHasMany(snapshot, relationship.key, relationship)) {
this._super.apply(this, arguments);

const key = relationship.key;

if (!json[key]) {
json[key] = [];
}
Expand Down Expand Up @@ -84,7 +84,7 @@ export default DS.RESTSerializer.extend({
});
return attributes;
},

extractRelationships(modelClass) {
let relationships = this._super(...arguments);

Expand All @@ -93,7 +93,20 @@ export default DS.RESTSerializer.extend({
relationships[key] = { links: { related: key } };
}
});

return relationships;
}
},

});

// DEPRECATION: The private method _shouldSerializeHasMany has been promoted to the public API
// See https://www.emberjs.com/deprecations/ember-data/v2.x/#toc_jsonserializer-shouldserializehasmany
if( ! DS.JSONSerializer.prototype.shouldSerializeHasMany ) {
Serializer.reopen({
_shouldSerializeHasMany( snapshot, key, relationship ){
return this.shouldSerializeHasMany( snapshot, key, relationship );
}
});
}

export default Serializer;

0 comments on commit c0424b6

Please sign in to comment.