jeudi 23 août 2018

Hiding deleted records from templates

I am trying to figure out if there's a better way to hide deleted records from a template. My ember app is listening to a web socket and if another user deletes a record, there's a handler that removes that record from the store:

_handleDelete({id, type: modelName}) {
  const record = get(this, 'store').peekRecord(modelName, id);

  if (record) {
    get(this, 'store').deleteRecord(record);
  }
}

What I'm currently doing is defining a computed property that filters out any records where isDeleted is true, and using that for other collections:

allItems: computed.filter('items.@each.isDeleted', function(item) {
  return !get(item, 'isDeleted');
}),

Then I'm using this computed property to build other computed properties:

materialCosts: computed.filterBy('allItems', 'proposalCostType', 'material'),

This works fine, but the issue I'm having is that I didn't start with this, so many of my templates are referencing the hasMany relationship directly (items), rather than the filtered collection (allItems). It's a huge app so I'd have to update a TON of templates to get this working, and I don't think that's time efficient.

I never want a deleted record to ever display in the template, so ideally I'd be able to do something where the items relationship automatically filters out deleted records and is updated when they are removed.

I'm on Ember Data 2.14 so maybe there's a newer version that can do this?




Aucun commentaire:

Enregistrer un commentaire