【问题标题】:Ember computed property not updating view when the firstObject is removed删除 firstObject 时,Ember 计算属性不更新视图
【发布时间】:2015-01-18 21:13:39
【问题描述】:

我有一个项目 cmets 列表,我的模板显示了最新的,带有一个按钮,可以调出一个模式来显示所有 cmets。在该模态中,您可以单独删除 cmets,这很有效,并且模态会相应更新。模板也会更新,直到我删除最后一条记录。

这是我最初的财产声明:

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

即使我查询控制器并确认 firstObject 未定义,计算的属性仍然保持其值。

如果我将计算属性更改为像这样观看@each 评论:

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

然后视图确实更新了,但控制台输出了一个丑陋的错误:

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 

当最后一条记录被删除时,如何更新视图?还是我应该忽略这个错误?

【问题讨论】:

    标签: ember.js properties computed-properties


    【解决方案1】:

    如何使用

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

    【讨论】:

    • facepalm 是的,这行得通,让我不必看两个属性。但是,它并没有消除错误,我不确定是否应该让它发生,因为一切似乎都运行良好,或者该错误的真正原因是什么。
    • 当您不包含mostRecentComment 时,错误就会消失?
    猜你喜欢
    • 2014-11-29
    • 2012-08-04
    • 1970-01-01
    • 2014-03-01
    • 2018-12-11
    • 1970-01-01
    • 2016-04-22
    • 2014-04-06
    • 2012-12-08
    相关资源
    最近更新 更多