灰烬可观察的地图

时间:2014-09-04 07:53:43

标签: ember.js

这应该很容易,但我无法弄清楚。我正在创建一个有点像三态多选的视图,核心组件是视图上的一个属性,它列出了每个选项及其状态:

TriState = Ember.View.extend({
    ...
    childView: TristateItem,
    selectionStates: {}
});

TristateItem = Ember.View.extend({
    ...
    selectedState: function() {
        var value = this.get('value');
        var states = this.get('parentView.selectionStates');
        var state = states[value];
        if (Ember.isNone(state))
            return 0;
        return state;
    }.property('value', 'parentView.selectionStates'),

    click: function(event) {
        var states = this.get('parentView.selectionStates');
        var value = this.get('value');
        var newState = states[value];
        if (Ember.isNone(newState) || newState == 0)
            newState = 1;
        else if (newState == 1)
            newState = -1;
        else
            newState = 0;
        states[value] = newState;
        event.stopPropagation();
    }

因此,点击几下后,selectionStates的值可能是

{0: 1,   // value "0" is selected with state "1"
 3: -1,  // value "3" is selected with state "-1"
 4: 0}   // value "4" is not selected

这样可以正常工作,但TristateItem.selectedState的值不会改变。我认为这是因为不可能观察到javascript对象,但是在Ember中找不到任何可观察的东西。我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

由于您正在设置属性,因此您可以使用notifyPropertyChange以某种方式通知其他计算属性父对象是脏的。

this.notifyPropertyChange('parentView.selectionStates');

http://emberjs.jsbin.com/tokese/1/edit