Relationships and Changesets
See original GitHub issueWhen working with changesets, this pattern is quite common:
let home = new Address();
let work = new Address();
let user = new User();
user.addresses = [home, work];
let validator = function() {};
let model = Changeset(user, validator);
<form>
<h3>Personal details</h3>
{{input type='text' value=model.firstName}}
<!-- etc… -->
<h3>Addresses</h3>
{{#each model.addresses as |address|}}
{{input type='text' value=address.street}}
{{input type='text' value=address.city}}
<!-- etc… -->
{{/each}}
</form>
What I’d like to do here is to make changes to the address instances and buffer them inside a changeset. So that I can do rollback on the model and automatically have rollbacks applied to all child changesets too.
Maybe by specifying which relationships should create proxy changesets and buffer, e.g. let model = Changeset(user, validator, { nested: ['addresses'] }); or similar (opt-in).
Right now changesets seems to support access via keys, e.g. changeset.set('profile.url', '…') but not changeset.get('profile').set('url', '…')
-
Has there been any discussion on supporting relationships, such that the handlebars example above would work with rollback/save on the user, with changes applied to nested addresses?
-
Would also be good to support rollback/apply for changes to the collection itself, i.e. adding/removing addresses. Maybe via an API such as
model.get('addresses').add(vacationHome);wheremodel.get('addresses')would return an instance of e.g.ChangeSetList.
Related issues:
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:9

Top Related StackOverflow Question
@sandstrom and @snewcomer, just to confirm your instincts on this; I would also find this feature very useful. I work around it by creating my own
Changesetand define a property on that that overrides the original array and returns an array of changesets for each item in the original array. Equally, I override thesave(),rollback(),isPristineandisDirtyon the parent changeset to take into account the new array of child changesets. Having the feature @sandstrom described would save me quite a lot of boilerplate code.@snewcomer Thanks for taking time answering!
One idea could be to extend the basic principle of the changeset to relationships.
It’s already a proxy at the key/value surface of an object, basically get/set is proxied and buffered. But this “leaks” is if
myChangeset.childrenreturn an array (or an array-like relationship object from Ember Data).One approach would be to “tell” the changeset, when initiated, about the keys that return relationships, for example
new Changeset(object, { listKeys: ['children'] }). For these keys, instead of proxying down to the underlying model the changeset could create aChangeSetList(on-the-fly), associate this with the changeset and wrap it around the relationship [array or similar] and return it as the response.myChangeset.childrenwould then return aChangeSetListwith methods such asaddandremove, plus support iteration, e.g. viatoArrayorSymbol.iterator. Adding something to the changeset-list, viaadd, would then buffer/proxy just likegetandsetalready does on a changeset. This would allow rollback/save of added/removed relationship items.Happy to clarify my thinking about this and explain in more detail!