Skip to content

Commit

Permalink
Incorporate rsutphin’s serialiser fix
Browse files Browse the repository at this point in the history
  • Loading branch information
backspace committed Dec 5, 2014
1 parent 3973465 commit 170638b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
9 changes: 5 additions & 4 deletions app/initializers/data-populator.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ export default {

issue.save().then(function() {
var feature = store.createRecord('feature', {title: 'A feature'});
issue.get('features').pushObject(feature);
feature.save();
issue.save().then(function() {
application.advanceReadiness();
feature.save().then(function() {
issue.get('features').pushObject(feature);
issue.save().then(function() {
application.advanceReadiness();
});
});
});
}
Expand Down
2 changes: 1 addition & 1 deletion app/models/issue.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import DS from 'ember-data';

export default DS.Model.extend({
title: DS.attr('string'),
features: DS.hasMany('feature'),
features: DS.hasMany('feature', {inverse: 'issue'}),

rev: DS.attr('string')
});
42 changes: 42 additions & 0 deletions app/serializers/application.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Taken from https://gist.github.com/rsutphin/3252dfd221157d557a0b

import Ember from 'ember';
/*globals EmberPouch*/

export default EmberPouch.Serializer.extend({
/**
* Copied from JSONSerializer to enable default serialization of the hasMany
* side of manyToOne relationships. It is not clear to me why this is not the
* default — it seems to make it impossible to ever load the many from the one —
* but there is plentiful evidence that it's not going to be changed:
*
* - https://github.com/emberjs/data/commit/7f752ad15eb9b9454e3da3f4e0b8c487cdc70ff0#comments
* - https://github.com/emberjs/data/issues/2068
* - https://github.com/emberjs/data/pull/1678
*
* The supported solution is to use a custom serializer for each model and
* explicitly indicate which hasMany relationships should be embedded as IDs.
* No thank you.
*/
serializeHasMany: function(record, json, relationship) {
var key = relationship.key;

if (this._canSerialize(key)) {
var payloadKey;

// if provided, use the mapping provided by `attrs` in
// the serializer
payloadKey = this._getMappedKey(key);
if (payloadKey === key && this.keyForRelationship) {
payloadKey = this.keyForRelationship(key, "hasMany");
}

var relationshipType = record.constructor.determineRelationshipType(relationship);

if (relationshipType === 'manyToNone' || relationshipType === 'manyToMany' || relationshipType === 'manyToOne') {
json[payloadKey] = Ember.get(record, key).mapBy('id');
// TODO support for polymorphic manyToNone and manyToMany relationships
}
}
}
});

0 comments on commit 170638b

Please sign in to comment.