Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knockout best practices. If or visible

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.

like image 752
user3272018 Avatar asked May 06 '15 21:05

user3272018


People also ask

How do you write if condition in Knockout?

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.

What happens in visible binding?

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 .

What is observable in Knockout?

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.


2 Answers

It really depends on the scenario. From the docs:

if plays a similar role to the visible binding. The difference is that, with visible, the contained markup always remains in the DOM and always has its data-bind attributes applied - the visible binding just uses CSS to toggle the container element’s visiblity. The if 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.

like image 147
David Sherret Avatar answered Sep 25 '22 12:09

David Sherret


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 -->
like image 37
Andrew Avatar answered Sep 22 '22 12:09

Andrew