Replace add/remove events with a single sort event when a model is changes #28
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When a model that exists in a obscura sorted collection changes and the change affects the sorted index of the model in the collection, the model is removed and then added to the collection. This effectively makes sure it ends up in the right place.
The problem with this approach is that downstream listeners to the collection will receive a remove followed by the add event. Some data binding libraries that work with Backbone collections (such as Epoxy) receive these events and wastefully destroy and recreate the View that is associated to the model that has changed.
The solution I came up with for this surpasses the add/remove events, and triggers a 'sort' event on the collection afterward. Subscribers to the 'sort' event typically know how to do deal with models that gets moved around in a collection. This feels a bit hacky, as we're fudging a 'sort' event, but since Obscura is essentially an extension to the Backbone collection this should be OK.
This PR seems to pass all of the established unit tests. I may need to write some to test this functionality though?
This might not be the best way to do this so it'd be great to get some input. It would also be good to hear if this solution has the potential to cause problems. I did notice that this 'sort' event eventually turns into a 'reset' event as it passes through the Filtered/Pagination collections. So this something that might need to be addressed.