-
Notifications
You must be signed in to change notification settings - Fork 108
Added option to allow localStorage to update first and return successful immediately. #86
base: master
Are you sure you want to change the base?
Conversation
…ful immediately. Remote sync is then achieved by calling `syncDirtyAndDestroyed()`.
@@ -27,6 +28,7 @@ Backbone.Collection.prototype.syncDestroyed = -> | |||
for id in ids | |||
model = new @model(id: id) | |||
model.collection = @ | |||
model.remoteFirst = true if (result(model, 'localFirst') or result(model.collection, 'localFirst')) |
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.
model.remoteFirst = (result(model, 'localFirst') or result(model.collection, 'localFirst'))
is better?
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.
Or this:
model.remoteFirst = result(model, 'localFirst') or result(model.collection, 'localFirst')
There are lots of ways to skin a cat. I don't have a strong preference in this case.
So Would you like to follow the approach in #65 (comment), in which a local |
|
@@ -16,6 +16,7 @@ Backbone.Collection.prototype.syncDirty = -> | |||
|
|||
for id in ids | |||
model = if id.length == 36 then @findWhere(id: id) else @get(id) | |||
model.remoteFirst = true if (result(model, 'localFirst') or result(model.collection, 'localFirst')) | |||
model?.save() |
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.
Instead of setting an attribute on the model, what do you think about passing in a special option to save, such as save(remoteFirst: true)
? It should get passed through to sync/dualStorage. Then you also won't have to clean it up later.
I like the direction you're going with this. Sorry I couldn't get to reviewing it until now. I want to give some thought to the names localSyncFirst and remoteSyncFirst. Maybe it's fine though. Go ahead and clean up based on the comments. |
This will need to have unit tests before we can pull it in. |
The server response should not completely overwrite the collection as the collection might have non-persisted models in it.
…d if in the same store.
Apologies about the delay in response. I've just pushed some more changes to my forked repo. Goals in this version:
From my manual testing everything seems to work OK. Unfortunately I'm not quite sure how to go about setting up the unit/integration tests to fit with the existing tests. If anyone is able to help out with this, even just to point me in the right direction, that would be greatly appreciated. Also, I will need to refactor some of the code to be able to merge. There is also some code duplication that will need to be refactored. Please let me know if I have overlooked anything or you have any suggestions on how to improve this push. |
Is this very much different than the approach in linked comment? #65 (comment) With current nilbus/Backbone.dualStorage master that code makes it work local first, and if online it syncs after. Only weird thing is the two events being fired, for local and remote saves. Also, for slow connections I added following snippet, which cancels "long" running connections, please let me know what you think: # A phone with Sim card but no plan thinks it has connection, same situation
# as with very slow connections. This makes jQuery less tolerant with slow
# connections, raises a connection error effectively switching dualStorage
# to offline.
$.ajaxSetup
timeout: 2000 That way the app configures itself according to the type of connection it has. |
@tute Yes, this is the same approach in that a local sync will occur first and then the online sync is added to the request queue. Have you had a chance to try it out? Thanks for the Ajax timeout settings. I'm actually using that in my app already but it might be nice to integrate it as an option for the request queue. |
I didn't try the fork, @maxfi, but if we can mimic similar to same behavior with 5 lines of code it seems like the option to implement/iterate. Thanks for your input! |
Haha. I agree @tute! I'll have to checkout your code and see if it does what I'm looking for. |
I haven't looked at the localSyncFirst implementation in great detail yet. However I won't be able to accept this as-is because of the massive code duplication between |
Remote sync is then achieved by calling
syncDirtyAndDestroyed()
.