alekseykulikov / backbone-offline

[Deprecated] Allows your Backbone.js app to work offline
MIT License
720 stars 56 forks source link

Not Syncing Automatically? #14

Closed seabre closed 12 years ago

seabre commented 12 years ago

I am very new to Backbone.js. So, please excuse my ignorance. From what I understand, the with backbone.offline data in localstorage is supposed to sync automatically when fetch() is called. For some reason, this isn't working for me.

However, when I manually call this.storage.sync.push() or @storage.sync.push it works great. My model without the manual call for the push looks something like this:

class MyApp.Models.MyModel extends Backbone.Model
  paramRoot: 'my_model'

  toJSON: ->
    json = _.clone(this.attributes)
    _.extend(json, {my_model_images_attributes: this.get("my_model_images")})

class MyApp.Collections.MyModelsCollection extends Backbone.Collection
  model: MyApp.Models.MyModel
  url: '/my_models'
  initialize: ->
    @storage = new Offline.Storage('my_models', this, autoPush: true)
    @fetch()

To add the manual call for the push I change my MyModelsCollection to look like this:

class MyApp.Collections.MyModelsCollection extends Backbone.Collection
  model: MyApp.Models.MyModel
  url: '/my_models'
  initialize: ->
    @storage = new Offline.Storage('my_models', this, autoPush: true)
    @fetch()
    setInterval(@pushChanges, 10000)

  pushChanges: =>
    @storage.sync.push()

Backbone is sitting on top of a Rails 3.2 app, and, if it would be helpful at all, I used rails-backbone to generate the initial scaffold for the backbone app.

seabre commented 12 years ago

I think it was an issue with caching. Sync with the fetch works now.