javascript - 当绑定(bind)到深度嵌套的属性时,Ember 计算属性不会触发

标签 javascript ember.js coffeescript

JSBin隔离我在代码中遇到的问题。我有一个嵌入式模型的层次结构和一个计算属性 (data),只要链最底部的值发生变化 (symbol),它就会触发。该示例直接显示属性以及计算属性的结果。按钮会更改单击时的值。您会看到它更新了属性,但计算属性没有触发。为什么 selectedAllocation.positions.@each.instrument.symbol 无法在任何 instrument.symbol 更改时触发计算?

如果这个例子看起来做作,那只是因为我试图抽象出现实中更复杂的东西,例如这些数组中不止一个对象,data 是必需的,因为另一个库需要一个特定格式的简单 JS 对象。

最佳答案

Note that @each only works one level deep. You cannot use nested forms like todos.@each.owner.name or todos.@each.owner.@each.name.

http://emberjs.com/guides/object-model/computed-properties-and-aggregate-data/

你需要创建一个别名来将 symbol 提升一个级别(不是 coffeescript 的人,希望你能通读黑客,下面的 positions 别名是对于踢球和咯咯笑,没有区别)。

App.Position = Ember.Object.extend({
  instrumentSymbol: Em.computed.alias('instrument.symbol')
})

App.IndexController = Ember.ArrayController.extend
  selectedAllocation: null
  positions: Em.computed.alias('selectedAllocation.positions'),

  data: (->
    @get("positions").map (position) ->
      symbol: position.get "instrumentSymbol"
  ).property "positions.@each.instrumentSymbol"
  ...

http://jsbin.com/bivoyako/1/edit

关于javascript - 当绑定(bind)到深度嵌套的属性时,Ember 计算属性不会触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23985343/

相关文章:

javascript - DockYard Ember Validations 自定义日期验证器

javascript - emberjs 嵌套 {{#each}} 不起作用

javascript - 将 WebGL 非连续线渲染为单个对象

django - 在 Django 中使用模板变量和 django-compressor

javascript - 单击一个 div 以使用 javascript 在其中切换一个复选框

javascript - jQuery 窗口调整大小无法正常工作

javascript - 如何从另一个 Controller (而不是从模板)访问另一个 Controller 的属性

ruby-on-rails - Backbone + Coffeescript + Rails App Init 问题(2 级命名空间): Cannot read property 'IndexView' of undefined

Javascript - EXTjs 访问数组值

javascript - jQuery click() 事件不会在 AJAX 加载的 HTML 元素上触发