Last active
September 6, 2015 21:09
-
-
Save Keeo/3f7eb3e0dce2bafbe6eb to your computer and use it in GitHub Desktop.
This file contains 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
// 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(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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!