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

Fix _shouldSerializeHasMany deprecation #185

Merged
merged 1 commit into from
Jun 22, 2017
Merged
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
Fix _shouldSerializeHasMany deprecation
  • Loading branch information
broerse committed Jun 22, 2017
commit f63f15a0b5bbda736f3eeffea2965a34d2d616ab
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;