Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember observable map

Tags:

ember.js

This should be easy, but I can't figure it out. I am creating a view that behaves a bit like a tri-state multiple select, and the core component is a property on the view which lists each option and its status:

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();
    }

So after a few clicks, the value of selectionStates might be

{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

This works fine, but the value of TristateItem.selectedState doesn't change. I assume that this is because it is not possible to observe a javascript object, but I can't find anything observable in Ember that is equivalent. How can I do this?

like image 757
aquavitae Avatar asked Dec 05 '25 19:12

aquavitae


1 Answers

Since you're setting the property, you can just use notifyPropertyChange to inform the other computed property that the parent object is dirty in some way.

this.notifyPropertyChange('parentView.selectionStates');

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

like image 50
Kingpin2k Avatar answered Dec 08 '25 08:12

Kingpin2k



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!