Created
March 20, 2015 01:40
-
-
Save dnegstad/b5f31b218e6197cd76e4 to your computer and use it in GitHub Desktop.
Ember Polymorphic Serialization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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