-
-
Notifications
You must be signed in to change notification settings - Fork 141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improved Ember Data and Ember Changeset support for relationships #599
Comments
Also here is a working implementation using the Reference API and extending ember-changeset. (thanks Oliver!)
|
Just wanted to say that I totally encourage thinking about this! It's not a simple problem, but solve it would be very useful!🏅
Source: #551 (comment) |
@sandstrom Appreciate the comments! So in considering both approaches...
Perhaps they may both work in concert but do I have the overall train of thought right? Do you have any specific concerns with, say the |
My comments are a bit confusing, because I'm throwing several ideas around. But I guess I have two main ideas (1 and 2) when thinking of the overall problem with tracking changes to graphs of Ember Data models.
Your suggestion above (#599 (comment)) would probably also work well, but is somewhat different from the ideas (1) and (2). So I'm not trying to argue against it. I merely wanted to present two alternative ways of approaching this. But you are in a much better position to determine a good way forward than I am 😄 // these classes are far from complete, but hopefully help illustrate the idea
class ChangeSetList {
constructor(changeSet, items) {
this.items = items;
this.changeSet = changeSet;
this.addedItems = new Set();
this.removedItems = new Set();
}
add(item) {
this.addedItems.add(item);
}
remove(item) {
if (this.addedItems.has(item)) {
// don't track, not part of original items
this.addedItems.delete(item);
} else {
this.removedItems.add(item);
}
}
// synthetic list based on non-deleted and added items
toArray() {
this.items.filter((itm) => {
this.removedItems.has(itm);
}).concat(this.addedItems);
}
hasChanges() {
this.addedItems.size > 0 || this.removedItems.size > 0;
}
}
class ChangeSet {
constructor(source, options = {}) {
this.source = source;
this.listKeys = options.listKeys || [];
this.instantiatedLists = new Map();
}
get(key) {
if (this.listKeys.includes(key)) {
if (this.instantiatedLists.has(key)) {
return this.instantiatedLists.get(key);
} else {
let list = new ChangeSetList(this, )
this.instantiatedLists.set(key, list);
return list;
}
} else {
// run whatever logic ChangeSet has today
}
}
set(key, value) {
if (this.listKeys.includes(key)) {
throw new Error(`Cannot set a list key, use myChangeSet.get('${key}').add(itm), or .remove(itm) instead`);
} else {
// run whatever logic ChangeSet has today
}
}
} |
We could check in with @richbrown @oliverlangan @Gorzas and see if they have any thoughts. |
Thx for the comments! Data changes are probably a no go. The complexity around tracking changes is quite large. I believe there is a lot of simplicity coming. But not there yet. RE: your ideas - I think this is probably good for a different conversation. This issue is specific to detecting and using the existing Ember Data APIs to work with relationships - even singular belongsTo relationships. Your ideas are probably nice to think about/tackle once we can properly detect a |
What I described in point 2 shouldn't need any awareness of Ember Data, i.e. it shouldn't matter if the object is a POJO with an array value for one of the keys, or if the object is an Ember Data model. |
I don't have enough experience with Ember Changeset to have a clear idea so I don't know if I really can help. Anyway, if you need somebody to test the changes, I'll be glad to help you with it 😄 . |
Hi, @snewcomer, thanks a lot for all the (great) work on this lib. Again, thanks a lot! |
@pernambucano (and anyone else who's interested in this), consider sponsoring @snewcomer via Github sponsors, so he can spend more time on this library (ask your employer to pay). We sponsor him with $100 every month. |
@snewcomer thanks for this awesome fix 🥳 we are currently upgrading to Ember-Data 5.x and we had huge troubles with ember-changeset. The idea of @snewcomer saved us and now we can continue our upgrade! |
Main problem
Create a changeset with a
model
, set a belongsTo relationship, change a belongsTo, and lastly revert back to original - the changeset will be dirtySupport for ember-data relationship objects has been fraught with issues. Precisely and often with async (default) relationships in Ember Data. This is due to them being behind a Proxy, thus a
===
will fail with the same relationship.This issue is to track (and keep me honest) in either implementing better support for ember data relationships or improved documentation.
One idea that is floating around is to use @embroider/macros to see if e-d is installed and then we can
importSync
the model and do aninstanceof Model
check. I'll be exploring this space in the next few weeks.The text was updated successfully, but these errors were encountered: