Backbone.dualStorage
Backbone.dualStorage copied to clipboard
dualsync() should return a jQuery promise (or Deferred)
Default Backbone implementation states that it is returning a jqXHR object. jQuery also documents that a jqXHR objects implement the Promise interface, giving them all the properties, methods, and behavior of a Promise (see Deferred object for more information)
So it would be very useful that dualsync also returns a Promise.
Since the API is synchronous, the returned promise would have been already resolved.
I like this idea.
I do this with following code
# dualSync Promise adapter
Backbone.dualSync = Backbone.sync
Backbone.sync = (method, model, options) ->
result = Backbone.dualSync.apply(this, arguments)
if result.then
result
else
# localSync never fails, so we return success promise
(new $.Deferred()).resolve(result).promise()
The main question is if it's true that localsync never fails. Better approach would be to wrap localSync into Promise properly
I do it with a fetch override
fetch: =>
$.Deferred((deferred) => super
success: deferred.resolve
error: deferred.reject).promise()
I believe dualsync will already return the jQuery xhr promise when online, and we only need to create a new promise when localsync handles the sync by itself. I'll write some specs to make sure this is true.