Ember.js Data how to clear datastore

风流意气都作罢 提交于 2019-12-22 04:09:49

问题


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?


回答1:


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




回答2:


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




回答3:


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




回答4:


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



回答5:


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.




回答6:


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.




回答7:


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



来源:https://stackoverflow.com/questions/14903361/ember-js-data-how-to-clear-datastore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!