5

I'm observing a data resurrection after localStorage.clear() or deleteRecord() (jsbin example) is called in my ember app.

Follow these steps to replicate the problem:

  1. Open up chrome web dev --> Resources --> Local Storage --> "http://jsbin.com"
  2. Add some orgs on this page and observe that they are added to local storage
  3. Click on "Clear local storage" (case 1) or "Delete Org data" (case 2) and observe that the local storage is cleared. Do not refresh the page. (Data still remain on the page but that's okay because we haven't refreshed the page.)
  4. Add one more org and observe that all the deleted orgs are resurrected (case 1) or LS is not deleted (case 2)
  5. So when you refresh the page, ALL THE DELETED DATA ARE RESURRECTED.

Questions:

  1. Why does this data resurrection happen?
  2. How can I make localStorage.clear() work even without a refresh after clear (either manually or calling location.reload)?

edited later to include deleteRecord()

HaoQi Li
  • 11,970
  • 14
  • 58
  • 77

2 Answers2

2

Problem fixed in September's version of Ember Data/Local Storage. See it in action in this SO Answer with a jsbin.

After some discussions with folks on IRC, there is currently a bug in LSAdapter or in Ember Data that causes the data to linger around.

The problem is on pause as of August 2013 due to imminent changes in Ember Data.

The work-around for this problem is to both clear the Local Storage as well as the ember data. See it working in this jsbin.

Alternatively, you can immediately call location.reload() after clearing local storage to show the fresh data.

Community
  • 1
  • 1
HaoQi Li
  • 11,970
  • 14
  • 58
  • 77
1

Wiping localStorage with clear works just OK. It is your local storage adapter (LSAdapter) that keeps the state and repopulate previously cleared localStorage on creating new record. This is done that way because records are stored in a string and LSAdapter don't know if you've changed it or not, so it always save all the records to be sure it is synced.

Consider destroying all the records instead of clearing localStorage.

chrmod
  • 1,415
  • 12
  • 19
  • Does this mean that LSAdapter does not get data directly from Local Storage every time? Is the string stored in the `Store`? – HaoQi Li Aug 08 '13 at 20:36
  • while reading LSAdapter code you can see that `localStorage` methods are called only in two cases: 1) in `_saveData` that 'serialize' all records and put them to localStorage as a String 2) in `_loadData` which gets the String from localStorage and populate LSAdapted `_data` property with your records. The `_loadData` is called only once during LSAdapter initialization (`init` method) – chrmod Aug 08 '13 at 20:42
  • Thanks! Does [`model.destroy()`](http://stackoverflow.com/a/15171990/733034) still work? It doesn't work [when I tried it](http://jsbin.com/iwiruw/311) and I can't find where `destroy` is defined in [Ember model.js](https://github.com/emberjs/data/blob/master/packages/ember-data/lib/system/model/model.js#L28). – HaoQi Li Aug 08 '13 at 20:51
  • Ember Model has a method `destroyRecord` which you call upon your record. Then you have to call `commit` on your store to make that change persistent. It is working good on your jsbin. – chrmod Aug 08 '13 at 21:08
  • 2
    (Just for reference, it's `deleteRecord`, maybe you can edit your comment.) – graup Aug 08 '13 at 21:20
  • thanks for correcting me out @graup - cannot update it though. – chrmod Aug 08 '13 at 21:49