rundeks
rundeks

Reputation: 103

Ember.js Data how to clear datastore

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?

Upvotes: 10

Views: 11953

Answers (7)

Frank Treacy
Frank Treacy

Reputation: 3716

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

Upvotes: 18

mpowered
mpowered

Reputation: 13526

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.

Upvotes: 0

Michael
Michael

Reputation: 12011

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.

Upvotes: 0

Samarpan
Samarpan

Reputation: 36

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);
    }
  }
});

Upvotes: 1

blueFast
blueFast

Reputation: 44361

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

Upvotes: 6

HaoQi Li
HaoQi Li

Reputation: 12350

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

Upvotes: -2

ahmacleod
ahmacleod

Reputation: 4310

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

Upvotes: 4

Related Questions