Skip to main content

Basic Usage

What Is Knockout?

Knockout.js is a MVVM JavaScript framework used in Magento 2 UI Components and frontend modules.

When to Use

Use Knockout when:

  • Creating dynamic UI components (e.g., cart, checkout).
  • Binding frontend logic to observable data.
  • Using Magento's UI Components (e.g., <uiComponent> XML).

Example

<input type="text" data-bind="value: name, valueUpdate: 'afterkeydown'" />
<p data-bind="text: name"></p>
define(['uiComponent'], function(Component) {
return Component.extend({
initialize: function () {
this._super();
this.name = ko.observable('Default');
}
});
});