I'd like to use a property on my ViewModel to toggle which icon to display without creating a separate computed property of the inverse. Is this possible?
<tbody data-bind="foreach: periods">
<tr>
<td>
<i class="icon-search" data-bind="visible: !charted, click: $parent.pie_it"></i>
<i class="icon-remove" data-bind="visible: charted, click: $parent.pie_it"></i>
</td>
</tr>
</tbody>
My ViewModel has a property periods which is an array of month, like this:
var month = function() {
this.charted = ko.observable(false);
};
It's little confusing, as you have to do
so, i did
my model is
check in fiddle http://jsfiddle.net/khanSharp/bgdbm/
In order to make the binding aware of changes to the property, I copied the visible binding handler and inverted it:
Disclaimer: this solution is for entertainment purposes only.
Also can use hidden like this:
I agree with John Papa's comment that there should be a built-in
hidden
binding. There are two benefits to a dedicatedhidden
binding:hidden: charted
instead ofvisible: !charted()
.charted
directly, rather than creating acomputed
to observe!charted()
.It's simple enough to create a
hidden
binding, though, like this:You can use it just like the built-in
visible
binding:I was having the same issue about how to use an opposite of a boolean observable. I have found an easy solution:
Now on your HTML you should do this
When the program starts only "Text1" is visible because "false===false is TRUE" and Text2 is not visible.
Lets say we have a button which invokes the gatherPlacesData on click event. Now Text1 won't be visible because "true === false is FALSE" and Text 2 only be visible.
Another possible solution could be using computed observable but, I think is a overcomplicated solution for a so simple problem.