Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
95
rated 0 times [  100] [ 5]  / answers: 1 / hits: 22436  / 13 Years ago, tue, january 10, 2012, 12:00:00

I would like to bind a visible property to be true when one of two conditions are true. Something like the following



 <tr data-bind=visible: active || $parent.displayDeactive>....</tr>


My code works when I do one or the other bindings but not when I put the || in there. I haven't found any documentation that says I can put any logic in this binding, but if I can't do it directly what is the est way to do it since I am binding a property of a template and one object of the $parent viewmodel.


More From » knockout.js

 Answers
1

If you are using the value of an observable in an expression then you need to reference them as a function. So, if active and displayDeactive are observables you would do:



data-bind=visible: active() || $parent.displayDeactive()



There are a few ways to move it to the view model, you could:




  • create a computed observable on the child (function would need to be able to reference the parent)

  • create a function on the parent that takes in the child and returns your value (bindings are executed in a computed observable, so it will fire again when any observable that it accesses changes)

  • create a function on the child that takes in the parent and returns the value (same note as above)



Sample of logic in the binding and using a function on the parent here: http://jsfiddle.net/rniemeyer/f6ZgH/


[#88112] Monday, January 9, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dequant

Total Points: 88
Total Questions: 99
Total Answers: 95

Location: Ukraine
Member since Sun, Dec 13, 2020
4 Years ago
;