nilbus/Backbone.dualStorage — pull request #113 by DatenMetzgerX (2014-06-01 14:18:16+00:00)
Title: parse / toJSON applied inconsistent #111

Fix for the issue parse / toJSON applied inconsistent #111

The solution is more a workaround then a real solution... The code base should be refactored. The local sync should only works with json objects and not with real model instances. This way we could avoid the multiple parsing / serializing of the objects.

The current solution should ensure, that parse and toJSON are applied in a consistent manner. The support for parseBeforeLocalSave has not been extended and has been marked as deprecated. The main reason is, that the localSync should only be a back end that stores the values locally and should behave the exact same way as the remote endpoint. So we should not need an additional method here. The use case mentioned in the other issue is still supported, as you can see in the unit test.

* Override collection.parse to parse the result on the client
* Override toJSON on the collection to be able to write back to the server (not supported by the solution with parseBeforeLocalSave)
* Override parse in the model (if needed). Also override parse in the model.

The implementation ensures, that parse is always passed the JSON representation of the model, independent if the data has been retrieved from the server or from local storage.

Room for improvements
* parse / toJSON is invoked way to often (update: 2x parse, at least once toJSON)
* solution is a workaround
* parseBeforeLocalSave should not be needed
* additional unit tests

The checked in source works for my project I'm working on. To perform the complete refactoring I'm missing the insight and the time...

I hope this solution works for you. If you have any questions or improvements, I will gladly answer them.
Micha

How can I assign a pull request to an issue?