node package manager
Stop wasting time. Easily manage code sharing in your team. Create a free org »


Aurelia Knockout Plugin

npm Version Build Status Greenkeeper badge


Platform Support

This library can be used in the browser only.


Install this plugin with npm:

npm install aurelia-knockout --save

Integration with Aurelia CLI

To get started with the Aurelia CLI you have to add some lines to aurelia.json under aurelia_project after installed this plugin through npm:

Add this snippet to the dependencies array in aurelia.json:

      "name": "aurelia-knockout",
      "path": "../node_modules/aurelia-knockout/dist/amd",
      "main": "aurelia-knockout"


Register the plugin at the startup of Aurelia:

export function configure(aurelia) => {

Binding syntax

To use the knockout binding syntax in your HTML view you have to add the "knockout" custom attribute to a html element which surrounds all elements which are using knockout syntax:

    <div knockout>
        <button data-bind="click: changeVisibility">Change Visibility</button>
        <div data-bind="if: isVisible">
            <span data-bind="text: firstName"></span>
            <span data-bind="text: lastName"></span>

This will apply all bindings from this element and its children to the current viewmodel. The bindingContext from Aurelia's "bind" function will be used for that. You have to add the attribute to each view with knockout syntax.


If you used Durandal as your previous SPA-Framework you want to use compositions. You can see the syntax below. As the syntax looks like knockout, the compose binding was not part of knockout itself. This plugin can handle the old Durandal composition syntax.

    <div knockout>
        <button data-bind="click: switchView">Switch SubView</button>
        <div data-bind="compose: view"></div>
        <div data-bind="compose: 'testModel2.html'"></div>

All cases from the offical Durandal documentation should be covered:

<div data-bind="compose: 'path/to/view.html'"></div>
<div data-bind="compose: 'path/to/module'"></div>
<div data-bind="compose: { view: 'path/to/view.html' }"></div>
<div data-bind="compose: { model: 'path/to/module' }"></div>
<div data-bind="compose: { model: moduleInstance }"></div>
<div data-bind="compose: { view: 'path/to/view.html' model: 'path/to/module' }"></div>
<div data-bind="compose: { view: 'path/to/view.html' model: moduleInstance }"></div>
<div data-bind="compose: moduleInstance"></div>
<div data-bind="compose: moduleConstructorFunction"></div>

You can also pass an object as activationData which is passed as argument to the activate(activationData) function:

<div data-bind="compose: { model: 'path/to/module', activationData: data }"></div>

The composition logic can handle Knockout Observables as parameters as well. The previous composition will be replaced. The following callback functions will be called:

compositionComplete(element, parentElement) if the Aurelia completes the composition and detached(element, parentElement) before the composed view will be removed from the DOM.

Set values from @bindable properties

To ensure full flexibility for your migration process, this plugin provides also a feature to set @bindable properties in rewritten Aurelia sub-views from the activationData which comes from old Knockout based views:

Parent Knockout based view
  <div data-bind="compose: { model: 'path/to/submodule', activationData: data }"></div>

The data object looks like:

  price: ko.observable(5),
  productName: "Apples"
Child Aurelia based view
  Product: <span>${productName}</span>
  Price: <span>${price}</span>

The backing JavaScript code:

import {bindable} from "aurelia-framework";
import {KnockoutBindable} from "aurelia-knockout";
import {inject} from 'aurelia-dependency-injection';
export class ProductView {
    constructor(knockoutBindable) {
        this.knockoutBindable = knockoutBindable;
    activate(activationData) {
        this.knockoutBindable.applyBindableValues(activationData, this);

This will set the value from activationData.price to this.price. this.productName however, is not updated because it has no @bindable decorator and the variable from activationData is no Knockout Observable. To process non Knockout Observables anyway you have to pass false as third parameter to the applyBindableValues function. If the outer value changed (and is an Observable) the corresponding inner variable is updated too.

Subscriptions for Knockout Observables which are created from this plugin internally will be disposed automatically if the child view is unbound.

Building The Code

To build the code, follow these steps.

  1. Ensure that NodeJS is installed. This provides the platform on which the build tooling runs.
  2. From the project folder, execute the following command:
npm install
  1. To build the code, you can now run:
npm run build
  1. You will find the compiled code in the dist folder, available in these module formats: AMD, CommonJS, ES2015, ES2017, Native Modules and System.

  2. See package.json for other tasks.

Running The Tests

To run the unit tests, first ensure that you have followed the steps above in order to install all dependencies and successfully build the library. Once you have done that, proceed with this additional steps:

  1. You can run the tests with this command:
npm run test