【问题标题】:Observer in custom Ember view not always triggered on controller property change自定义 Ember 视图中的观察者并不总是在控制器属性更改时触发
【发布时间】:2014-03-13 10:12:19
【问题描述】:

我想根据多个条件过滤一组对象。也可以进行排序。到目前为止我的设置:http://emberjs.jsbin.com/OxIDiVU/284/edit

排序和过滤工作。但是,当切换过滤器时,我无法可靠地更新我的过滤器复选框视图。只要封闭控制器中的filterStop 数组发生变化,就应该触发函数filterChanged

部分代码摘录:

App.FilterCheckbox = Ember.Checkbox.extend({
    'data-count': null
    isActive: false
    init: () ->
        # filterChanged only triggered if we set a breakpoint in the init function :/?
        this._super()
    filterChanged: (() ->
        active = this.get('controller.filterStop')[this.get('data-count')] == true
        this.set('isActive', active)
    ).observes('controller.filterStop.@each')
});

在我的控制器中,我有一个更改 filterStop 的操作:

filterByStops: (count) ->
    old_value = this.get('filterStop')[count]
    this.get('filterStop').replace(count, 1, [!old_value])

现在奇怪的是,filterChanged 观察者函数并不总是被触发。如果我在FilterCheckbox.init 函数中设置断点并在断点被命中后继续执行,观察者总是会触发(如预期的那样)。如果我删除断点并重新加载页面,观察者将不再触发。

任何想法为什么这种行为取决于断点?如何设置它,以便我的复选框可以观察其封闭控制器的属性?

更新: 我刚刚注意到调试器断点技巧仅适用于带有 chrome 开发工具的 chrome。在带有 Firebug 的 Firefox(用于 OSX)中,技巧/作弊不起作用。

【问题讨论】:

    标签: ember.js


    【解决方案1】:

    Okey,所以我发现我只需要明确告诉视图要使用哪个控制器实例。更新后的jsbin:http://emberjs.jsbin.com/OxIDiVU/287/edit

    还有我的查看代码:

    {{view App.FilterCheckbox data-count="2" controller=controller}}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多