Skip to content
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

[BUGFIX beta] prevent record duplication due to EmbeddedRecordsMixin #4441

Closed
wants to merge 3 commits into from

Conversation

tarzan
Copy link

@tarzan tarzan commented Jun 22, 2016

This is a proposed fix to #1829

When the EmbeddedRecordsMixin is used to persist embedded records to the backend, the parent record is handled as expected. However, nested children are neglected in the run loop and upon pushing a returned payload from the server into the store we end up with duplication. This commits adds a procedure for cleaning up these flagged 'uncommitted' records from the store before pushing the created ones.

@tarzan
Copy link
Author

tarzan commented Jun 22, 2016

This is my first try at fixing this issue that has been haunting our software at times. Please let me know what you guys think. :)

types.forEach(function (type) {
var existingRecords = store.peekAll(type);
var uncommitted = existingRecords.filter(function (record) {
return record.get('isNew') && !record.get('isSaving');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this will unload all record of a type the isNew state even if they were not the records sent in the original request?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct. It does. There currently is no test that fails showcasing that scenario, but it's easy to write one.

Problem is, I am stumbling with a way to let the store know which records are being persisted to the backend. Should the EmbeddedRecordMixin flag the records by changing their currentState to 'inFlight'? How should I handle this in the Ember data runloop?

@tarzan
Copy link
Author

tarzan commented Jun 24, 2016

Ok. So I added a test that fails due to my current implementation. Can anyone offer some advice on how to improve it?

@bmac
Copy link
Member

bmac commented Jun 24, 2016

@igorT I think you had some ideas on how handle this?


run(function() {
post = store.createRecord('post', { name: 'The Parley Letter' });
post2 = store.createRecord('post', { name: 'Another Parley Letter' });
Copy link
Contributor

@sbatson5 sbatson5 Jun 24, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't look like post2 is defined above on line 2643 like post.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, the linter fails 'cause of that as well. It has no bearing on the failing of this test, however.

@tarzan tarzan force-pushed the embedded-duplications-fix branch from 3140ba9 to ecbf416 Compare July 4, 2016 13:37
@tarzan
Copy link
Author

tarzan commented Jul 4, 2016

@jcbvm correct. That's why there still is a failing spec. I am still looking for suggestions on how to handle this. Problem is that the EmbeddedRecordsMixin is mixed into the serializer and at this point we have to flag the records that are being embedded and persisted this way. Then when the store commits the action we have to unload the flagged records or merge them with the newly received ones from the response-payload.

… proposed fix to emberjs#1829

When the EmbeddedRecordsMixin is used to persist embedded records to the backend, the parent record is handled
as expected. However, nested children are neglected in the run loop and upon pushing a returned payload from the server
into the store we end up with duplication. This commits adds a procedure for cleaning up these flagged 'uncommitted' records
from the store before pushing the created ones.
@tarzan tarzan force-pushed the embedded-duplications-fix branch from ecbf416 to 33df55e Compare July 12, 2016 17:02
@tarzan
Copy link
Author

tarzan commented Jul 12, 2016

Ok. I changed it around a bit. Now the embedding of a (hasmany) record that has not yet persisted, will get a dummy 'embedded' ID. As a result, the store will be able to find these records when processing the payload.

To me, this implementation seems a bit hacky, but I'm having a hard time coming up with a different approach. Any feedback will be greatly appreciated!

@igorT how does it differ from the ideas you have?

@tarzan
Copy link
Author

tarzan commented Oct 20, 2016

Any news on this?

@jcbvm
Copy link

jcbvm commented Oct 23, 2016

@tarzan see emberjs/rfcs#173

emoryy added a commit to emoryy/data that referenced this pull request Feb 3, 2017
@runspired
Copy link
Contributor

The duplication issue can be resolved via use of lid and identifiers when the IDENTIFIERS ff is turned on #6366 . See emberjs/rfcs#403

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants