Nikash
Nikash

Reputation: 38

Knockout nested observable comparison to a boolean

The visible binding is evaluating to the wrong value despite all possible combinations of paranthesis!

My ViewModel is as below:

function ViewModel() {
    this.current_convo = ko.observable(new Message(false));
} 

function Message (val) {
    this.archived = ko.observable(val);
}

And my HTML reads

<button data-bind="visible: (current_convo().archived == false)">Archive</button>

I also tried using

<button data-bind="visible: !(current_convo().archived)">Archive</button>

I'm sure I'm missing something but unable to put my finger on it!

Upvotes: 0

Views: 332

Answers (1)

Ismail Badawi
Ismail Badawi

Reputation: 37207

The first won't work because archived is an observable -- it might evaluate to false, but it's not itself false. The second won't work, again because you're negating the observable, not its value.

This should work:

<button data-bind="visible: !current_convo().archived()">Archive</button>

Upvotes: 2

Related Questions