The Top 10 Mistakes That KnockoutJS Developers Make

1 Modules

Unlike other UI frameworks, Knockout does not come with a built in way to modularize your application. It’s possible to write an entire Knockout app in a single HTML file:

kojs_full

While this runs, it definitely won’t scale to applications of any significant complexity. The official Knockout docs suggest using RequireJS for Asynchronous Module Definition (AMD), and I wholeheartedly second this recommendation. Using RequireJS and the domReady module, the above code refactors into three separate files.

Good.html:

goodhtml

main.js

mainjs

viewModel.js

viewmodeljs

2 var self = this;

Keeping track of this in Knockout can be tough. Setting var self = this; at the top of every view model makes it easy.

For example, if var self = this; is not set then this must be passed into every computed observable:

viewmodeobj

Forgetting to pass in this is easy to do and can result in hard to track down bugs. Ifvar self = this; is set then passing this is not necessary:

viewmodelself

var self = this; can also help avoid bugs when one binding context refers to another. Consider the following code that displays a list of names. When a user clicks on an name in the list, that name is displayed in the <span> below.

HTML:

html

Javascript:

js

When selectPerson is called its scope is set to the object in person corresponding to the name clicked. However, because var self = this; is set the view model object is still accessible within the event callback. This would not be the case otherwise.

In production code I almost always define var self = this;. However, for the sake of brevity, I won’t use it in some examples in this article.

3 Logic in templates

Knockout makes it easy to write complicated logic in HTML templates. Avoid doing this at all costs! It results in difficult to test and maintain code. Instead of using logic in templates…

computedlogic

4 Testing

Speaking of testing, by default Knockout offers no tools to help you. This does not mean you shouldn’t write tests! There are ample JS testing frameworks, and you should be able to pick up and drop any of them into your Knockout application. As with anything, the earlier you start writing tests the better. I usually don’t start a new Knockout project without also including a testing framework.

5 ko.mapping

ko.mapping is a plugin for Knockout that makes working with data fetched from REST endpoints much more enjoyable.

Without ko.mapping, a view model that consumes objects from a REST API might look like this:

    function viewModel() {
        /*
        * Gets data about a person from the server:
        * person = {
        *   firstName: "John",
        *   lastName: "Stewart"
        * }
        */
        var person = getPersonFromServer();

        this.person = {};
        this.person.firstName = ko.observable(person.firstName);
        this.person.lastName = ko.observable(person.lastName);
    }

With ko.mapping the view model is much simpler:

    function viewModel() {
        var person = getPersonFromServer();
        this.person = ko.mapping.fromJS(person);
    }

Want to convert part of the view model back to plain JSON so that you can POST it back to the API? That’s a one-liner too:

    var json = ko.mapping.toJS(this.person);

While ko.mapping is to some degree officially part of Knockout (it’s in the official documentation), it is a separate project that you’ll have to download and include.

It’s also worth nothing that some people prefer the functionally analogousko.viewModel plugin to ko.mapping. ko.mapping has always worked fine for me, but it only seemed fair to link to both. Use one or the other, but don’t use neither!

6 textInput and valueUpdate

This may seem minor, but it’s caused me so many headaches that it’s worth including. Knockout binds the value of input elements a somewhat strange way. To illustrate, let’s say we have an input element and want to update some text on the page in real time as the input’s value changes:

    <input type="text" data-bind="value: myValue">
    Value: <span data-bind="text: myValue"></span>
    function viewModel() {
        this.myValue = ko.observable();
    }

Surprisingly, the above won’t work. The myValue observable (and thus our text in the span) updates whenever the input loses or gains focus, not as it’s value changes. Why? For performance reasons this is how Knockout behaves by default.

To fix this in Knockout 3.2 (current at the time of writing) or newer, use the textInput binding instead of the value binding.

    <input type="text" data-bind="textInput: myValue">

This will provide immediate updates to myValue with the minor caveat that it won’t guarantee that the value attribute of the input is always synced with myValue.

If you are using an older version of Knockout or if you are dead set on using the value binding, then you must use the valueUpdate flag.

    <input type="text" data-bind="value: myValue, valueUpdate='afterkeydown'">

In Knockout 3.2 there are 4 possible values for valueUpdate, each with its own idiosyncrasies:

  • + input – Updates when the value of the element changes. Doesn’t work in IE8-.
  • + keyup – Updates when a keyup event is fired
  • + keypress – Updates when a keypress event is fired.
  • + afterkeydown – Updates as soon as the user starts typing a character. Does not work on all mobile browsers — notably Safari on IOS7.

Using the textInput binding is strongly recommended over using the valueUpdateflag.

7 Don’t repeatedly push into observable arrays

Pushing multiple times into observable arrays can cause significant performance issues with your application:

    function viewModel() {
        var arr = [0, 1, ..., 999];

        this.numbers = ko.observableArray();

        for (var i = 0; i < arr.length; i++) {
            this.numbers.push(arr[i]);
        }
    }

Running the for loop above will cause Knockout to redraw the page 1000 times — one for each push. To avoid this, simply overwrite the old value of our entire array:

    function viewModel() {
        var arr = [0, 1, ..., 999];
        this.numbers = ko.observableArray(arr);
    }

To transform data before putting it into our new array, use ko.utils.arrayMap.

    function Number(number) {
        this.number = number;
    }

    function viewModel() {
        var arr = [0,1, ..., 999];

        // creates an array of Number objects
        this.numbers = ko.observableArray(ko.utils.arrayMap(arr, function(number) {
            return new Number(number)
        }));

    }

To append many items to an existing observable array, exploit the fact that pushaccepts a variable number of arguments and use apply to push them in all at once.

    function viewModel() {
        var arr = [500, 501, ... 999];

        this.numbers = ko.observableArray([0, 1, ..., 499]);
        this.numbers.push.apply(self.numbers, arr);
    }

8 Observable arrays don’t automatically have observable members

Observable arrays track changes to which objects are in the array. They do not track changes to the state of those objects.

    <ul data-bind="foreach: people">
        <li data-bind="text: name"></li>
    </ul>
    <button data-bind="click: makeDerekMike">Make Derek Mike</button>
    function viewModel() {
        var self = this;

        self.people = ko.observableArray([
            {
                name: 'Derek',
            },
            {
                name: 'Sara'
            },
            ...
        ]);

        self.makeDerekMike = function() {
            for (var i = 0; i < self.people().length; i++) {
                if (self.people()[i].name === 'Derek') {
                    self.people()[i].name = 'Mike';
                }
            }
        };
    }

Clicking the button in the first example will not trigger a page refresh despite the fact that it will modify an object in the array underlying people. This is because the same objects still belong topeople. To trigger a page refresh, we need to explicitly make the fields of the elements in people observable:

    function viewModel() {
        var self = this;

        self.people = ko.observableArray([
            {
                name: ko.observable('Derek')
            },
            {
                name: ko.observable('Sara')
            },
            ...
        ]);

        self.makeDerekMike = function() {
            for (var i = 0; i < self.people().length; i++) {
                if (self.people()[i].name() === 'Derek') {
                    self.people()[i].name('Mike');
                }
            }
        };
    }

9 Components

Because Knockout makes building interactive widgets so simple, it’s easy to write similar code in different parts of your application. Use Knockout components to abstract common UI widgets and promote code reuse.

Using components, we can build a simple reusable list builder as follows:

    <span>This is a list with no initial values</span>
    <div data-bind="component: 'list-builder'"></div>

    <span>This is a list with some initial values</span>
    <div data-bind="component: {
        name: 'list-builder',
        params: { list: ['item', 'another item'] }
    }"></div>
    function Item(text) {
        this.text = text;
    }

    ko.components.register('list-builder', {
        viewModel: function(params) {
            var self = this;

            self.list = ko.observableArray([]);

            if (params && params.list) {
                self.list(ko.utils.arrayMap(params.list, function(item) {
                    return new Item(text);
                }));
            }

            self.newText = ko.observable();
            self.add = function() {
                self.list.push(new Item(text));
                self.newText('');
            };
        },
        template:
            '<ul data-bind="foreach: list">' +
                '<li data-bind="text: text"></li>' +
            '</ul>' +
            '<input type="text" data-bind="textInput: newText" />' +
            '<button data-bind="click: add">Add</button>'
    });

Note that you probably don’t want to inline the viewModel and template as above in any production code code. Fortunately, this can be solved using require.js (see #1). For a description of how to do this check out the official components documentation.

10 Tutorials

Knockout the official tutorials are great! They offer a interactive sandbox to play around with the framework in a guided fashion.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s