Search code examples
ember.jsember-data

How to ensure Ember Data remove the cache from store?


I've got a controller that shows the model count

const BlahController = Ember.Controller.extend({
    totalRows: Ember.computed('model.[]', function() {
        let items = this.get('model') || [];
        return items.get('length');
    }),
    ...
})

Then when I delete the model I do it using destroyRecord and even ensure that it's unloaded

model.destroyRecord().then(() => {
    this.get('store').unloadRecord(model);
}

Any idea why totalRows is never updated? I would've thought that by calling store.unloadRecord it will actually remove the model from the store and updates the model array in the controller?

The route hook model is something like this

const BlahRoute = Ember.Route.extend({
    model() {
        return this.store.findAll('blah-model');
    },
})

Solution

  • Turns out I was using a mixin in my route, and within that mixin the model function actually created a new array using this.store.peekAll().filter() which obviously won't be updated as the array now points to another object /facepalm