📜  KnockoutJS-声明式绑定

📅  最后修改于: 2020-10-23 07:36:08             🧑  作者: Mango


KnockoutJS中的声明性绑定提供了一种将数据连接到UI的强大方法。

重要的是要了解绑定和可观察对象之间的关系。从技术上讲,这两个是不同的。您可以使用普通的JavaScript对象作为ViewModel,而KnockoutJS可以正确处理View的绑定。

如果没有Observable,则仅在第一次处理UI中的属性。在这种情况下,它无法基于基础数据更新自动更新。为此,绑定必须引用可观察的属性。

绑定语法

绑定包括2个项目,绑定名称。以下是一个简单的例子-

Today is : 

在此,文本是绑定名称,whatDay是绑定值。您可以用逗号分隔多个绑定,如以下语法所示。

Your name: 

在此,在按下每个键后更新值。

绑定值

绑定值可以是单个值字面量变量,也可以是JavaScript表达式。如果绑定引用了无效的表达式或引用,那么KO将产生一个错误并停止处理绑定。

以下是绑定的一些示例。


Enter employee name:

请注意以下几点-

  • 空格没有任何区别。

  • 从KO 3.0开始,您可以跳过绑定值,这将使绑定具有不确定的值。

绑定上下文

当前绑定中使用的数据可以由对象引用。该对象称为绑定上下文

上下文层次结构由KnockoutJS自动创建和管理。下表列出了KO提供的不同类型的绑定上下文。

Sr.No. Binding Context Types & Description
1

$root

This always refers to top level ViewModel. This makes it possible to access top level methods for manipulating ViewModel. This is usually the object, which is passed to ko.applyBindings.

2

$data

This property is lot like this keyword in Javascript object. $data property in a binding context refers to ViewModel object for the current context.

3

$index

This property contains index of a current item of an array inside a foreach loop. The value of $index will change automatically as and when the underlying Observable array is updated. Obviously, this context is available only for foreach bindings.

4

$parent

This property refers to parent ViewModel object. This is useful when you want to access outer ViewModel properties from inside of a nested loop.

5

$parentContext

The context object which is bound at the parent level is called $parentContext. This is different from $parent. $parent refers to data. Whereas, $parentContext refers to binding context. E.g. you might need to access the index of outer foreach item from an inner context.

6

$rawdata

This context holds raw ViewModel value in the current situation. This resembles $data but the difference is, if ViewModel is wrapped in Observable, then $data becomes just unwrapped. ViewModel and $rawdata becomes actual Observable data.

7

$component

This context is used to refer to ViewModel of that component, when you are inside a particular component. E.g. you might want to access some property from ViewModel instead of current data in the template section of component.

8

$componentTemplateNodes

This represents an array of DOM nodes passed to that particular component when you are within a specific component template.

以下术语在绑定中也可用,但实际上不是绑定上下文。

  • $ context-这只是现有的绑定上下文对象。

  • $ element-该对象引用当前绑定中DOM中的元素。

处理文字和外观

以下是KO提供的用于处理文本和视觉外观的绑定类型的列表。

Sr.No. Binding Type & Usage
1 visible:

To show or hide HTML DOM element depending on certain conditions.

2 text:

To set the content of an HTML DOM element.

3 html:

To set the HTML markup contents of a DOM element.

4 css:

To apply CSS classes to an element.

5 style:

To define the inline style attribute of an element.

6 attr:

To add attributes to an element dynamically.

使用控制流绑定

以下是KO提供的“控制流绑定”类型的列表。

Sr.No. Binding Type & Usage
1 foreach:

In this binding, each array item is referenced in HTML markup in a loop.

2 if:

If the condition is true, then the given HTML markup will be processed. Else, it will be removed from DOM.

3 ifnot:

Negation of If. If the condition is true, then the given HTML markup will be processed. Else, it will be removed from DOM.

4 with:

This binding is used to bind the child elements of an object in the specified object’s context.

5 component: OR component:

This binding is used to insert a component into DOM elements and pass the parameters optionally.

使用表单域绑定

以下是KO提供的“表单域绑定”类型列表。

Sr.No. Binding Type & Usage
1 click:

This binding is used to invoke a JavaScript function associated with a DOM element based on a click.

2 event:

This binding is used to listen to the specified DOM events and call associated handler functions based on them.

3 submit:

This binding is used to invoke a JavaScript function when the associated DOM element is submitted.

4 enable:

This binding is used to enable certain DOM elements based on a specified condition.

5 disable:

This binding disables the associated DOM element when the parameter evaluates to true.

6 value:

This binding is used to link respective DOM element’s value into ViewModel property.

7 textInput:

This binding is used to create 2-way binding between text box or textarea and ViewModel property.

8 hasFocus:

This binding is used to manually set the focus of a HTML DOM element through a ViewModel property.

9 checked:

This binding is used to create a link between a checkable form element and ViewModel property.

10 options:

This binding is used to define the options for a select element.

11 selectedOptions:

This binding is used to work with elements which are selected currently in multi list select form control.

12 uniqueName:

This binding is used to generate a unique name for a DOM element.