10.04.2018

Биндинг «if» — Knockout.js

Назначение

Биндинг if позволяет контролировать появление в документе участка разметки (и применения внутри него data-bind атрибутов), только если указанное выражение оценивается как true (или true-подобное значение вроде не-null объекта или не пустой строки).

if играет похожую на биндинг visible роль. Отличие состоит в том, что с visible, соответствующая разметка всегда остается в DOM и к ней всегда применены data-bind атрибуты — биндинг visible использует простой CSS для переключения отображения элемента контейнера. Однако биндинг if физически добавляет или удаляет содержащуюся в DOM разметку, и применяет биндинги к потомкам, только если выражение является true.

Пример 1

Данный пример показывает, что биндинг if может динамически добавлять и удалять участки разметки при изменении значения observable-переменной.

See the Pen Knockout if — Пример: отображение сообщения by Kirill (@skv1991) on CodePen.

Исходный код: Представление

<label><input type="checkbox" data-bind="checked: displayMessage" /> Display message</label>
 
<div data-bind="if: displayMessage">Here is a message. Astonishing.</div>

Исходный код: Модель представления

ko.applyBindings({
     displayMessage: ko.observable(false)
});

Пример 2

In the following example, the <div> element will be empty for “Mercury”, but populated for “Earth”. That’s because Earth has a non-null capital property, whereas “Mercury” has null for that property.

<ul data-bind="foreach: planets">
 <li>
 Planet: <b data-bind="text: name"> </b>
 <div data-bind="if: capital">
 Capital: <b data-bind="text: capital.cityName"> </b>
 </div>
 </li>
</ul>
 
 
<script>
 ko.applyBindings({
 planets: [
 { name: 'Mercury', capital: null }, 
 { name: 'Earth', capital: { cityName: 'Barnsley' } } 
 ]
 });
</script>

It’s important to understand that the if binding really is vital to make this code work properly. Without it, there would be an error when trying to evaluate capital.cityName in the context of “Mercury” where capital is null. In JavaScript, you’re not allowed to evaluate subproperties of null or undefined values.

Параметры

  • Main parameterThe expression you wish to evaluate. If it evaluates to true (or a true-ish value), the contained markup will be present in the document, and any data-bind attributes on it will be applied. If your expression evaluates to false, the contained markup will be removed from your document without first applying any bindings to it.If your expression involves any observable values, the expression will be re-evaluated whenever any of them change. Correspondingly, the markup within your if block can be added or removed dynamically as the result of the expression changes. data-bind attributes will be applied to a new copy of the contained markup whenever it is re-added.
  • Additional parameters
    • None

Примечание: Использование “if” без элемента-контейнера

Sometimes you may want to control the presence/absence of a section of markup without having any container element that can hold an if binding. For example, you might want to control whether a certain <li> element appears alongside siblings that always appear:

<ul>
 <li>This item always appears</li>
 <li>I want to make this item present/absent dynamically</li>
</ul>

In this case, you can’t put if on the <ul> (because then it would affect the first <li> too), and you can’t put any other container around the second <li> (because HTML doesn’t allow extra containers within <ul>s).

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

<ul>
 <li>This item always appears</li>
 <!-- ko if: someExpressionGoesHere -->
 <li>I want to make this item present/absent dynamically</li>
 <!-- /ko -->
</ul>

The <!— ko —> and <!— /ko —> comments act as start/end markers, defining a “virtual element” that contains the markup inside. Knockout understands this virtual element syntax and binds as if you had a real container element.

Зависимости

Нет, кроме ядра библиотеки Knockout.


Эксклюзивно для сайта skv1991.ru

Перепечатка и копирование строго запрещены.

Оригинал страницы

Опубликовано: 10/04/2018

Добавить комментарий

Ваш адрес email не будет опубликован.