Ember computed property not updating view when the firstObject is removed

285 views Asked by At

I have a list of project comments, and my template shows the latest, with a button that brings up a modal to show all the comments. In that modal you can delete comments individually, which works, and the modal updates accordingly. The template also updates up until I delete the last record.

Here was my original declaration of the property:

mostRecentComment: Ember.computed('projectComments.firstObject', function() {
  return get(this, 'sortedProjectComments.firstObject');
})

Even though I queried the controller and confirmed that the firstObject was undefined, the computed property still held its value.

If I change the computed property to watch @each comment like so:

mostRecentComment: Ember.computed('[email protected]', 'projectComments.firstObject', function() {
  return get(this, 'sortedProjectComments.firstObject');
})

then the view does update but the console dumps out an ugly error:

Error: No model was found for '0'
    at new Error (native)
    at Error.EmberError (http://localhost:4200/assets/vendor.js:18042:23)
    at Ember.Object.extend.modelFor (http://localhost:4200/assets/vendor.js:75114:19)
    at __exports__.default.JSONSerializer.extend.extractSingle (http://localhost:4200/assets/vendor.js:68260:28)
    at apply (http://localhost:4200/assets/vendor.js:23073:27)
    at superWrapper [as extractSingle] (http://localhost:4200/assets/vendor.js:22647:15)
    at __exports__.default.Ember.Object.extend.extractSave (http://localhost:4200/assets/vendor.js:67828:21)
    at __exports__.default.Ember.Object.extend.extractDeleteRecord (http://localhost:4200/assets/vendor.js:67779:21)
    at __exports__.default.Ember.Object.extend.extract (http://localhost:4200/assets/vendor.js:67665:37)
    at http://localhost:4200/assets/vendor.js:75760:32 

How do I get the view to update when the last record is deleted? Or should I just ignore this error?

1

There are 1 answers

2
Sam Selikoff On

What about using

mostRecentComment: Ember.computed.alias('sortedProjectComments.firstObject')