Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

comment foreach binding vs foreach binding in knockoutjs

In my HTML I can define these knockout foreach bindings:

<!-- ko foreach: customer -->
   <div data-bind="text: id" />
<!-- /ko -->

vs

<div data-bind="foreach: customer">
    <div data-bind="text: id" />
</div>

Where are the differences between those two approaches?

like image 317
Elisabeth Avatar asked Jun 12 '13 14:06

Elisabeth


People also ask

For what purpose do we use foreach binding in Ko?

Purpose. The foreach binding duplicates a section of markup for each entry in an array, and binds each copy of that markup to the corresponding array item. This is especially useful for rendering lists or tables.

What is binding in KnockoutJS?

A binding context is an object that holds data that you can reference from your bindings. While applying bindings, Knockout automatically creates and manages a hierarchy of binding contexts. The root level of the hierarchy refers to the viewModel parameter you supplied to ko. applyBindings(viewModel) .

What is two-way binding in KnockoutJS?

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.


2 Answers

Use native binding when a parent-child relationship exists within the binding section, like a ul and a li.

Use the comment syntax for containerless binding when your binding section does not have a parent-child relationship.

In your example you use the first code block because you are not trying to bind to a parent-child structure. All you want to do is just bind your customer data to a div, you shouldn't have to create a parent div and foreach through the customers and append another div inside of the parent div. It's more than you want to do.

Good use of containerless binding

<!-- ko foreach: customer -->
<section>
   <p data-bind="text: customer.name"></p>
   <p data-bind="text: customer.orderDate"></p>
</section>
<!-- /ko -->

However, if you have an ordered list you should use the native binding because it just makes sense.

Native

<ol data-bind="foreach: customer">
   <li data-bind="text: customer.name"></li>
</ol>

Containerless

<ol> 
   <!-- ko foreach: customer -->
       <li data-bind="text: customer.name"></li>
   <!-- /ko -->
</ol>

The second example just looks silly. You're adding more complexity for something that shouldn't be complicated.

like image 175
David East Avatar answered Oct 04 '22 23:10

David East


In some cases, you might want to duplicate a section of markup, but you don’t have any container element on which to put a foreach binding

To handle this, you can use the containerless control flow syntax, which is based on comment tags

Mode details on The "foreach" binding, Note 4: Using foreach without a container element

like image 22
Claudio Redi Avatar answered Oct 04 '22 23:10

Claudio Redi