I'm working on somebody else's code and I've noticed this in the HTML templates a few times and was just wondering what the false could be referring to. This is just a typical template/viewModel setup. It's not inside a loop or another if statement or anything like that.
Asked
Active
Viewed 1,566 times
0
-
That is if binding in ko. `if: false` will make sure those elements are not rendered. Looks to me like a dead code – Rajesh Mar 23 '17 at 09:49
-
This might help: http://stackoverflow.com/questions/11553999/how-to-template-if-else-structures-in-data-bound-views, http://knockoutjs.com/documentation/if-binding.html – Rajesh Mar 23 '17 at 09:50
-
Also note that this was replaced by `data-bind="if: variable()"` in ko 3.2 – Rajesh Mar 23 '17 at 09:51
-
I know that it's a knockout "if" binding but what I'm wondering is what "false" is referring to. As in I understand "ko if: display == false" or something like that but I don't understand is what "if: false" means. Like if whats false? – NeedsHelp Mar 23 '17 at 09:53
-
`false` is the condition but instead of providing an expression you provide the constant `false` (ie it is always false) – GôTô Mar 23 '17 at 09:54
-
`ko if: display == false` here `display === false` will yield a boolean value and based on that value elements will be rendered. Since its `if: false`, those elements will not be rendered at all in any condition and is a dead code – Rajesh Mar 23 '17 at 09:54
-
I see now. Thanks. I was just paranoid that I was missing something. – NeedsHelp Mar 23 '17 at 09:55
2 Answers
1
In my opinion you could use <!-- ko if:false -->
in two cases:
- While debugging you want to hide something without removing the code (which the developer thinks could be useful in the future). Or rather some part you want to disable just for testing and re-enable before saving the file in the repository (it might have been saved by mistake)
- You want to run code only if KO is disabled/not found/not loaded

GôTô
- 7,974
- 3
- 32
- 43
0
It's an if statement but seems to be redundant because it's simply saying if false then run the blow code but it's not checking anything. An example if statement in Knockout.js is...
<ul>
<li>This item always appears</li>
<!-- ko if: someExpressionGoesHere -->
<li>I want to make this item present/absent dynamically</li>
<!-- /ko -->
</ul>
Without seeing the full code it's hard to tell, but it's checking for something if it's false and showing the code that follows. An explanation of how it works, also from the same link above.
The if binding causes a section of markup to appear in your document (and to have its data-bind attributes applied), only if a specified expression evaluates to true (or a true-ish value such as a non-null object or nonempty string).