-
Notifications
You must be signed in to change notification settings - Fork 109
parse / toJSON applied inconsistent #111 #113
base: master
Are you sure you want to change the base?
Conversation
Thanks for implementing this; especially the tests. They will be helpful when eventually we refactor this into a final solution. Because it seems this isn't affecting a lot of our current users, I'm going to hold off on merging this until it has been refactored and is fully ready. I want leave this open though for people who are affected by the same issue. Keep us posted about if you have success or issues with this workaround. As for the deprecation, it sounds nice in theory to just have dualSync store exactly what the server sends and act no differently. The need for FYI, you can create a pull request over an existing issue by using github's hub command-line tool. |
backbone.dualstorage.coffee
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using Backbone.Model
here was intentional, because Backbone-relational has problems when there is more than one instance of the same model with the same id (see #75). This avoids that problem by not using the model's constructor which may inherit from the backbone-relational model prototype.
This case should be supported... If you look at the unit test, i have used exactly such a data structure as posted by kurtmilam's. You can override the parse method of the collection. This transforms the server response in the normal json format. Each item of the array is then passed to the parse method of the model. The use of model.constructor is required, so that the right toJSON methods are used. Otherwise we do not support inheritance correctly... But this should not be a problem, if we work with json objects internally |
Hmm, I'll have to take a closer look. I'll look in more depth after I |
Using Backbone.Model instead of the model type, for compatibility with Backbone-relational
Just chiming in on this one, I got here because dualStorage wasn't playing nice with backbone.paginator, since Paginator overrides [
{ "total_pages":7, "page":1 },
[
{ "id": 1, "title": "Lorem ipsum" },
{ "id": 2, "title": "Dolor sit amet"}
]
] Requiring that users implement their own parse functions can be problematic in cases like this where |
backbone.dualstorage.coffee
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I imagine this should be workingResponse
and not workingRespone
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ขอร้อง
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ยับยั้ง
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.
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
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?