alekseykulikov / backbone-offline

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

Storage.clear() and Storage.isEmpty() #16

Closed danharper closed 12 years ago

danharper commented 12 years ago

I have a collection 'tasks' with a storage key of 'tasks'.

tasks.storage.clear()
tasks.storage.isEmpty()
# returns false

tasks.storage.getItem('tasks')
# returns '' (empty string)

Surely tasks.storage.getItem('tasks') should return null?

I can't seem to make it so clear() removes the 'tasks' key. It always seems to be reset to an empty string.

It seems like the unit test:

it 'sets collection-key to ""', ->
  @storage.clear()
  expect(localStorage.getItem('dreams')).toEqual('')

Should actually be:

it 'sets collection-key to ""', ->
  @storage.clear()
  expect(localStorage.getItem('dreams')).toBeNull()

Any help with this? Or is clear() not supposed to empty the whole collection from storage? If you could point me in the right direction I could submit a pull request?

alekseykulikov commented 12 years ago

It's correct remark, thanks. The mistake is here:

clear: ->
  # ...
  this.setItem(@name, '')

I think we should fix this behavior in the next version.

danharper commented 12 years ago

I've tried removing that line, yet this still happens.

tasks.storage.clear()
tasks.storage.getItem('tasks')
# returns '' (empty string)

I've tried debugging, like so:

  clear: ->
    keys = Object.keys(localStorage)
    collectionKeys = _.filter keys, (key) => (new RegExp @name).test(key)
    this.removeItem(key) for key in collectionKeys
    # this.setItem(@name, '')
    console.log 'before reset', this.getItem(@name)
    record.reset() for record in [@allIds, @destroyIds]
    console.log 'after reset', this.getItem(@name)

# CONSOLE:
# => before reset, null
# => after reset, ''

:\

alekseykulikov commented 12 years ago

I'll try to figure out with it tomorrow.

danharper commented 12 years ago

Just noticed you fixed this, thank you :)