Skip to content
This repository has been archived by the owner on Apr 11, 2023. It is now read-only.

syncDirtyAndDestroyed() doesn't remove/clear ###_dirty in localStorage #129

Closed
Cinezaster opened this issue Jan 20, 2015 · 4 comments
Closed

Comments

@Cinezaster
Copy link

Hi,
After a syncDirtyAndDestroyed() on a collection the "###_dirty" still contains an id of a model. I get a status-code 200 from the server. Since dualStorage is not fetching models that are in the dirty list our app get stuck on models that are marked dirty.

I cannot find in the code where the ###_dirty is cleared

@nilbus
Copy link
Owner

nilbus commented Jan 20, 2015

Check out Store.clean and the places where clean is called, in localsync update and delete. Once you figure out the scenario that creates this error condition, we can create a failing spec that reproduces it and fix the issue.

@Cinezaster
Copy link
Author

If I run syncDirtyAndDestroyed(); should I pass through localsync function? Because it doesn't.

@nilbus
Copy link
Owner

nilbus commented Jan 21, 2015

Backbone.Collection.prototype.syncDirty = (options) ->
  store = localStorage.getItem("#{getStoreName(@)}_dirty")
  ids = (store and store.split(',')) or []

  for id in ids
    @get(id)?.save(null, options)

For each id marked dirty, it tries to get the model from the collection by that id. If a model with that id exists, it will try to save it. Save should call sync, which is overridden to be dualsync.

dualsync = (method, model, options) ->
  # ...

  # execute only online sync
  return onlineSync(method, model, options) if _.result(model, 'remote') or _.result(model.collection, 'remote')

  # execute dual sync
  # ...

If neither the model nor collection have a truthy remote property, then it will continue to try to dualsync. Double-check this.

As dualsync executes, localsync generally always gets called below backbone.dualstorage.coffee:283. Trace the execution to see what's actually happening.

@Cinezaster
Copy link
Author

Got it. The response on the PUT call is empty, but status is 200. Backbone handles this as an error but since the error is never logged I didn't know the call was not successful. I fixed this by adding

  Backbone.ajax = function() {
    var args = Array.prototype.slice.call(arguments, 0);
    if (args[0].type === 'PUT') {
        args[0].dataType = "text";
    }
    return Backbone.$.ajax.apply(Backbone.$, args);
};```


This is not a bug in this library. A bit of error feedback would be nice.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants