Magento custom Knockout.js bindings
This topic lists the custom Knockout.js bindings used in the core Magento files. These bindings can also be used by third-party developers.
General concepts
Aliases
The standard way to reference a knockout.js binding is using the data-bind
attribute: [data-bind="%binding_name%: %value%"]
. In Magento implementation, you can also use aliases to declare bindings. Some bindings may be defined as attributes ([%binding_alias%="%value%"]
) or nodes (%binding_alias% args="%value%">
).
Binding values
Apart from the value type specified for each binding, every value may be wrapped in Knockout’s observable.
Custom Magento bindings
afterRender
The afterRender
binding notifies its subscriber when an associated element is inserted into the DOM.
Source: <Magento_Ui_module_dir>/view/base/web/js/lib/knockout/bindings/after-render.js
. See on Github.
Value type: (element: Element, viewModel: Object) => void
.
Function that is invoked after the element is rendered.
Aliases: [afterRender]
Usage example:
autoselect
The autoselect
binding automatically highlights the text in an input element, when it gets focus.
Source: <Magento_Ui_module_dir>/view/base/web/js/lib/knockout/bindings/autoselect.js
. See on Github.
Value type: Boolean
Defines whether the binding is enabled (true
) or disabled (false
).
Aliases: [autoselect]
Usage example:
bindHtml
The bindHtml
binding renders the provided string, as a collection of HTML elements, inside of the associated node.
It also instantiates all bindings defined for the rendered elements in the scope of the current view model.
Source: <Magento_Ui_module_dir>/view/base/web/js/lib/knockout/bindings/bind-html.js
. See on Github.
Value type: String
Aliases: [bindHtml]
Usage example:
collapsible
The collapsible
binding provides methods and properties required for implementing collapsible panels. It can automatically collapse panel when clicking outside of the associated node, toggle optional CSS class when node changes its visibility. It has additional helper bindings: toggleCollapsible
, openCollapsible
and closeCollapsible
.
Source: <Magento_Ui_module_dir>/view/base/web/js/lib/knockout/bindings/collapsible.js
. See on Github.
Value type: Object
Binding’s configuration that may include the following properties:
Property | Description | Type | Default value |
---|---|---|---|
as
|
A key for accessing the binding in the current scope. | String |
$collapsible
|
closeOnOuter
|
Whether the panel needs to be closed on outside boundaries click. | Boolean |
true
|
onTarget
|
Toggles panel's visibility on the target node click. | Boolean |
false
|
openClass
|
CSS class that is added to or removed from the target node, when the panel changes visibility. If the option values is left empty, then the binding does not modify the element's classes. | String |
_active
|
Aliases: [collapsible]
datepicker
The datepicker
binding is an adapter for the mage/calendar.js widget.
Source: <Magento_Ui_module_dir>/view/base/web/js/lib/knockout/bindings/datepicker.js
. See on Github
Value type: String|Object
Aliases: -
Usage example:
fadeVisible
The fadeVisible
binding performs the gradual change of the element’s visibility (with an animation effect).
Source: <Magento_Ui_module_dir>/view/base/web/js/lib/knockout/bindings/fadeVisible.js
. See on Github.
Value type: Boolean
Defines whether the element is visible (true
) or hidden (false
).
Aliases: -
Usage example:
i18n
The i18n
binding is used to translate a string according to the currently enabled locale. Additionally, it creates the necessary elements for the TranslateInline jQuery widget, if it’s enabled on the page.
Source: <Magento_Ui_module_dir>/view/base/web/js/lib/knockout/bindings/i18n.js
. See on Github.
Value type: string
Aliases: [translate]
, <translate>
Usage example:
keyboard
The keyboard binding allows setting up listeners for the keypress
event of a specific key.
Source: <Magento_Ui_module_dir>/view/base/web/js/lib/knockout/bindings/keyboard.js
. See on Github.
Value type: [name: number]: (e: KeyboardEvent) => void
A collection in which keys represent keyboard keys codes and values are callback functions invoked when the associated key is pressed.
Aliases: [keyboard]
Usage example:
mageInit
The mageInit
binding is an adapter for the [data-mage-init]
attribute that is used to initialize jQuery widgets on the associated element.
Source: <Magento_Ui_module_dir>/view/base/web/js/lib/knockout/bindings/mage-init.js
. See on Github.
Value type: [path: string]:
Object
Aliases: -
Usage example: creating modal window
optgroup
The optgroup
binding is a decorator for the standard Knockout’s options binding which adds the support of nested options, and renders them as the <optgroup>
element.
Source: <Magento_Ui_module_dir>/view/base/web/js/lib/knockout/bindings/optgroup.js
. See on Github.
Value type: Array<string>
| Array<Object>
Aliases: -
Usage example:
outerClick
The outerClick
binding allows to subscribe for the “click” event that happens outside of the boundaries of the associated element.
Source: <Magento_Ui_module_dir>/view/base/web/js/lib/knockout/bindings/outer_click.js
. See on Github.
Value type: Function.
Callback that is invoked when user clicks outside of the element.
Aliases: [outerClick]
Usage example:
range
The range
binding is an adapter for the jQuery UI Slider widget. It also implements necessary handlers to work with mobile devices.
Source: <Magento_Ui_module_dir>/view/base/web/js/lib/knockout/bindings/range.js
. See on Github.
Value type: Object.
Configuration that is passed to the Slider widget.
Aliases: [range]
Usage example:
resizable
The resizable
binding is an adapter for the jQuery UI Resizable widget.
Source: <Magento_Ui_module_dir>/view/base/web/js/lib/knockout/bindings/resizable.js
. See on Github.
Value type: Object.
Configuration for the Resizable widget.
Aliases: [resizable]
Usage example:
scope
A binding that allows evaluating descendant nodes in the scope of an object found in the UiRegistry by provided string.
Source: <Magento_Ui_module_dir>/view/base/web/js/lib/knockout/bindings/scope.js
. See on Github.
Value type: String.
Component’s name by which to perform a lookup in the registry.
Aliases: [ko-scope]
, <scope>
Usage example:
staticChecked
The staticChecked
binding implements the behavior similar to the standard checked
binding. The difference is that staticChecked
doesn’t change the array of the already selected elements if the value of the associated DOM element changes.
Source: <Magento_Ui_module_dir>/view/base/web/js/lib/knockout/bindings/staticChecked.js
. See on Github.
Value type: Boolean | String | Number | Array<String|Number|Number>
Aliases: [staticChecked]
template
Magento template
binding is a customization of the existing Knockout template
binding. It is used to render a template inside of the associated element. The original Knockout’s implementation was overridden to support asynchronous loading of templates by the provided path, instead of searching for them on the page.
Source: <Magento_Ui_module_dir>/view/base/web/js/lib/knockout/template/engine.js
. See on Github.
Value type: String | Object
Сonfiguration for the template
binding. If the provided value is a string, it is processed as a path to .html
file.
Aliases: [render]
, <render>
Usage example:
tooltip
Magento custom knockout binding for displaying a tooltip.
Source: <Magento_Ui_module_dir>/view/base/web/js/lib/knockout/bindings/tooltip.js
. See on Github.
Value type: Object
Binding’s configuration that may include the following options:
Option | Description | Type | Default value |
---|---|---|---|
action |
An action that triggers displaying a tooltip. | String ('click'|'hover') | 'click' |
center |
Center the tooltip relatively to the element it is bound to. | Boolean | false |
closeButton |
Whether the tooltip has a Close button. | Boolean | false |
closeOnScroll |
Whether the tooltip closes automatically when user scrolls the page. | Boolean | true |
delay |
A delay before displaying the tooltip, in seconds. | Number | 0 |
position |
The priority position for the tooltip. | String ('top'/'right'/'left'/'bottom' ) |
'top' |
track |
Whether the tooltip moves together with the pointer | Boolean | false |
trigger |
The selector's action that triggers displaying a tooltip. | String | '' |
Aliases: [tooltip]
Usage example:
Adding the tooltip binding as an attribute:
Adding the tooltip binding as a node: