10

I am experiementing with Ember.js and have setup a small app where users can login and logout. When the user logs out I want to clear all of the currently cached records in the Data Store. Is there a way to do this or would I have to force the browser to reload the page?

MilkyWayJoe
  • 9,082
  • 2
  • 38
  • 53
rundeks
  • 103
  • 1
  • 1
  • 5

7 Answers7

18

I know this question is from 2013. But since Ember Data 1.0.0-beta.17 (May 10, 2015) there's a straightforward way of clearing the datastore:

store.unloadAll()

(More here: http://emberigniter.com/clear-ember-data-store/)

Frank Treacy
  • 3,386
  • 1
  • 16
  • 16
6

It looks like, as of today, there is still no generic way of fully forcing a store cleanup. The simplest workaround seems to loop through all your types (person, ...) and do:

store.unloadAll('person');

As seen here

blueFast
  • 41,341
  • 63
  • 198
  • 344
  • I don't know when it was introduced but the current documentation states that you can just do `store.unloadAll()` to unload all records. – Daniel May 31 '15 at 06:38
4

Clearing the data store is not yet supported in Ember-Data. There is an open issue concerning this on the Github tracker.

ahmacleod
  • 4,280
  • 19
  • 43
  • That issue is closed, but I still see no method of clearing the store without resetting the application: I need to clear the store for debugging purposes, potentially re-requesting data from the backend, without otherwise affecting the state of my application (I want to stay in the same route, for example). Is this possible? – blueFast Jun 25 '14 at 14:29
1

A cleaner & generic approach. Just extend or reopen store & add a clear method like this.

DS.Store.extend({
   clear: function() {
    for(var key in this.typeMaps)
    {
      this.unloadAll(this.typeMaps[key].type);
    }
  }
});
Samarpan
  • 36
  • 1
0

There is:

App.reset();

But that does more than clear out the data store and we've occasionally seen errors where store.pushPayload tries to push data onto an object marked destroyed from calling App.reset();.

Been we've been using:

store.init();

Which just creates a new empty store and works great but unfortunately is a private method.

Michael
  • 11,571
  • 4
  • 63
  • 61
0

This can now be done with store.destroy(). It unloads all records, but it also available for immediate use in reloading new records. I have confirmed this as of 1.0.0-beta.15. It doesn't appear to be in the documentation, but it's been working for me.

The alternative would be iterating the store's typeMaps and running store.unloadAll(typeMap.typeName), but I'm not sure it's entirely necessary.

mpowered
  • 13,162
  • 2
  • 15
  • 18
-2

Deleting record by record in model. deleteOrgs of this jsBin:

deleteOrgs: function(){
  var len;
  while(len = this.get('model.length')) {
    // must delete the last object first because 
    // this.get('model.length') is a live array
    this.get('model').objectAt(len-1).deleteRecord();
  }
  this.get('store').commit();
}

( As of August 2013, there is currently a problem with lingering deleted data. )

Community
  • 1
  • 1
HaoQi Li
  • 11,970
  • 14
  • 58
  • 77
  • 2
    This is deleting the records, which is *way more* than removing them from the store. This is potentially dangerous, causing permanent data loss in the backend ([Delete Records](http://emberjs.com/guides/models/creating-and-deleting-records/#toc_deleting-records)) – blueFast Jun 25 '14 at 14:26