alekseykulikov/backbone-offline

Storage.clear() and Storage.isEmpty()

danharper opened this issue · 4 comments

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?

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

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

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

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, ''

:\

I'll try to figure out with it tomorrow.

Just noticed you fixed this, thank you :)