Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to hide the screen while knockout js bindings are being built?

I'm a huge knockoutjs fan. I use it for all my web development now and simply love it. One thing that I've not been able to figure out though is how to hide the UI while the knockoutjs bindings are being built.

For example, I have a very robust user interface with lots of templates being used on my page. The problem that I'm noticing is that when the user first visits the page, they see all of my templates for a split second before the bindings kick in and hide them.

What is the best way to fix this problem? I've tried using helper classes to hide them, but then the templates are not able to be displayed using 'visible' and 'if' bindings unless I remove the helper class reference (ie. ui-helper-hidden).

like image 986
Luc Avatar asked Mar 02 '12 11:03

Luc


People also ask

What ensure that the binding does not hide text?

A gutter margin ensures the binding doesn't hide text. The default gutter position is left.

What is two-way binding in knockout JS?

KO is able to create a two-way binding if you use value to link a form element to an Observable property, so that the changes between them are exchanged among them. If you refer a simple property on ViewModel, KO will set the form element's initial state to property value.

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 .


2 Answers

I was just googleing for this, and after using the observable way, I thought of another approach:

<div style="display: none" data-bind="visible: true">   <ul data-bind="foreach: items">     <li data-bind="text: name"></li>   </ul> </div> 

You don't need an observable, the visible will always evaluate to true once the data binding is done.

like image 71
Jason More Avatar answered Sep 23 '22 19:09

Jason More


There are a couple of strategies that you can use here.

-One is to place all of your actual content into templates that live in script tags (does work fine with native templates). Within the template, you can then use control-flow bindings. This would be like:

<div data-bind="template: 'contentTmpl'"></div>  <script id="contentTmpl" type="text/html">    <ul data-bind="foreach: items">        <li data-bind="text: name"></li>    </ul> </script> 

-Another choice is to use style="display: none" on the container element along with a visible binding that can be tied to a loaded observable where you change the observable to true after the bindings have been applied.

like image 37
RP Niemeyer Avatar answered Sep 19 '22 19:09

RP Niemeyer