Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent Html showing before knockout binding is executed

Tags:

knockout.js

I am using the following knockout scripts in my Html:

<!-- kno ifnot: bla -->   <tr><td>some stuff</td></tr>  <!-- /ko --> 

The problem I have is that before the bindings are executed this row will show for a second or two.

How can I prevent this from happening?

like image 857
shenku Avatar asked Oct 31 '12 23:10

shenku


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 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 .

How do I use KnockoutJS in HTML?

It is very easy to use KnockoutJS. Simply refer the JavaScript file using <script> tag in HTML pages. A page as in the following image will be displayed. Click on download link and you will get the latest knockout.

What is Ko observable?

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.


Video Answer


1 Answers

Here's a simple trick. Just make your root element initially hidden and set the visible binding to true.

<div style="display: none;" data-bind="visible: true">     <!-- the rest of your stuff --> </div> 

As it's rendered, before knockout does its thing, it will be initially hidden. When the bindings are applied, knockout will override the style and make it visible.


Alternatively, you can throw your view into a script block and instantiate it through a template. The script blocks will not be rendered but will be visible when knockout renders the template.

<!-- ko template: 'myView' --><!-- /ko --> <script id="myView" type="text/html">     <!-- the rest of your stuff --> </script> 
like image 150
Jeff Mercado Avatar answered Sep 20 '22 20:09

Jeff Mercado