Skip to content

Instantly share code, notes, and snippets.

@Keeo
Last active September 6, 2015 21:09
Show Gist options
  • Save Keeo/3f7eb3e0dce2bafbe6eb to your computer and use it in GitHub Desktop.
Save Keeo/3f7eb3e0dce2bafbe6eb to your computer and use it in GitHub Desktop.
// created by https://github.com/decasia
rollbackAttributes: function() {
this._super();
this.rollbackRelationships();
},
rollbackRelationships: function() {
var model = this;
model.eachRelationship(function(name, meta) {
if (meta.kind === 'belongsTo') {
var oldId = model.get(`${name}Id`);
if (oldId) {
model.get(name).then(function(child) {
if (!(child && child.get('id') === oldId)) {
model.store.findRecord(name, oldId).then(function(originalRecord) {
model.set(name, originalRecord);
});
}
});
}
}
});
},
// keep a cache of all belongsTo ids before there are any user-initiated changes
// so we can roll back manually in `rollbackAttributes`
cacheRelationships: function() {
var model = this;
model.eachRelationship(function(name, meta) {
if (meta.kind === 'belongsTo') {
model.get(name).then(function(child) {
if (child && child.get('id')) {
model.set(`${name}Id`, child.get('id'));
}
});
}
});
},
ready: function() {
this.cacheRelationships();
}
@baongoc124
Copy link

Hi, I tried to use your gist but it doesn't work for my case. When cacheRelationships run, my "model.get(name)" always return a null child so it doesn't cache anything. My model looks like this http://pastebin.com/p70A2Yjk

Do you have any ideas on this? Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment