Skip to content

Instantly share code, notes, and snippets.

@dnegstad
Created March 20, 2015 01:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dnegstad/b5f31b218e6197cd76e4 to your computer and use it in GitHub Desktop.
Save dnegstad/b5f31b218e6197cd76e4 to your computer and use it in GitHub Desktop.
Ember Polymorphic Serialization
import Ember from 'ember';
export default Ember.Mixin.create({
normalize: function(type, hash, prop) {
if (hash.type) {
let typeKey = this.typeForRoot(hash.type);
let newType = this.store.modelFor(typeKey);
if (newType) {
type = newType;
}
}
return this._super(type, hash, prop);
}
});
import DS from 'ember-data';
export default DS.Store.extend({
push: function(type, data, _partial) {
var oldType = type;
var dataType = data.type;
var modelType = oldType;
if (dataType && (this.modelFor(oldType) !== this.modelFor(dataType))) {
modelType = dataType;
var oldRecord = this.getById(oldType, data.id);
if (oldRecord) {
this.dematerializeRecord(oldRecord);
}
}
return this._super(this.modelFor(modelType), data, _partial);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment