4
votes

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()

2
No problem. Sorry about that.War10ck
Dude no problem. :) I'm learning as well.HaoQi Li

2 Answers

2
votes

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.

1
votes

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.