Backbone.dualStorage icon indicating copy to clipboard operation
Backbone.dualStorage copied to clipboard

dualsync() should return a jQuery promise (or Deferred)

Open amuino opened this issue 12 years ago • 4 comments

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.

amuino avatar Oct 14 '13 09:10 amuino

I like this idea.

nilbus avatar Oct 14 '13 12:10 nilbus

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

alebedev avatar Nov 01 '13 11:11 alebedev

I do it with a fetch override

  fetch: =>
    $.Deferred((deferred) => super
      success: deferred.resolve
      error: deferred.reject).promise()

reubano avatar May 10 '14 19:05 reubano

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.

nilbus avatar May 14 '14 01:05 nilbus