I'm looking for knockout if or visible best practices in this case:
<div data-bind="visible: $root.obsVar()">
...
<input type="text" data-bind="value: $root.obsVar().someField" />
...
</div>
In case when $root.obsVar()
is undefined, error will be thrown. If you change visible
to if
, problem will miss, but it requires rewriting of the html. If there is a lot of murkup in div
, it takes a lot of time. Are there any reasons to change $root.obsVar().someField
to $root.getSomeFieldValue
that always returns correct value or undefined? Or maybe there are other techniques not to get overhead and avoid errors.
If the condition evaluates to true or true-like value, then the given HTML markup will be processed. Else, it will be removed from DOM. If the condition in the parameter contains an observable value, then the condition is re-evaluated whenever the observable value changes.
The visible binding shows or hides the target DOM element or widget depending on the View-Model value. If the value is true , the target DOM element is shown. If the value is false , the target DOM element is hidden—its display CSS attribute is set to none .
Knockout. js defines an important role when we want to detect and respond to changes on one object, we uses the observable. An observable is useful in various scenarios where we are displaying or editing multiple values and require repeated sections of the UI to appear and disappear as items are inserted and deleted.
It really depends on the scenario. From the docs:
if
plays a similar role to thevisible
binding. The difference is that, withvisible
, the contained markup always remains in the DOM and always has itsdata-bind
attributes applied - thevisible
binding just uses CSS to toggle the container element’s visiblity. Theif
binding, however, physically adds or removes the contained markup in your DOM, and only applies bindings to descendants if the expression is true.
In your scenario, the only way to prevent an error without using if
, when someField
is null, is to do something along these lines:
data-bind="value: ($root.obsVar() == null) ? null : $root.obsVar().someField"
That's really annoying to have to write each time you access a property. It makes the code harder to maintain and it makes it easier to make a mistake—especially when you're adding a new binding to an obsVar
's property because you will have to remember to do that null check.
Unless you see some clear performance benefits from using visible
in your situation, my recommendation would be to go with if
because then you only have to write the check in one place and not multiple places.
You might find you need to use both.
Use if
to remove the markup from the DOM - preventing possible JS errors, and so it doesn't take up space on the page.
And then use visible
to hide the element until the page is fully loaded.
<!-- ko if: whatever -->
<div data-bind="visible: true" style="display: none">
my content
</div>
<!-- /ko -->
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With