Book Image

KnockoutJS Essentials

By : Jorge Ferrando, Jorge F Ferrando
Book Image

KnockoutJS Essentials

By: Jorge Ferrando, Jorge F Ferrando

Overview of this book

Table of Contents (16 chapters)
KnockoutJS Essentials
About the Author
About the Reviewers


One of the hardest problems to solve when we build the user interface is to synchronize data that developers manage behind the scenes, in the code, and data that is shown to the user. The first step that developers made was to separate the presentation and the logic. That separation allowed developers to manage both sides better and separately. But communication between these two layers still became hard. That was because JavaScript was considered a non-important language and we used to use it just for validation. Then jQuery gave us a clue about how powerful this language could be. But still data was managed in the server and we just displayed static presentations. That makes the user experience poor and slow.

Over the last few years, a new kind of architectural pattern has emerged. It is called the MVVM pattern. Libraries and frameworks that use this kind of pattern make it easy for developers to synchronize views and data. One of these libraries is Knockout, and the framework that uses Knockout is named Durandal.

Knockout is a fast and cross-browser-compatible library that helps us to develop client-side applications with a better user experience.

Developers don't need to worry any more about data synchronization. Knockout binds our code to the HTML elements showing the state of our code to the user in real time.

This dynamic binding makes us forget about coding synchronization, and we can focus our effort on coding the important features of our application.

Nowadays, managing these kinds of frameworks is a must for front-end developers. In this book, you will learn the basics of Knockout and Durandal and we will go into the best design practices and patterns of JavaScript.

If you want to improve the user experience of your applications and create fully operative frontend applications, Knockout and Durandal should be your choice.

What this book covers

Chapter 1, Refreshing the UI Automatically with KnockoutJS, teaches you about the Knockout library. You will create observables and make your templates reactive to changes.

Chapter 2, KnockoutJS Templates, shows how to create templates to reduce your HTML code. Templates will help you keep your designs maintainable and they can be adapted to your data.

Chapter 3, Custom Bindings and Components, shows how to extend the Knockout library to make your code more maintainable and portable.

Chapter 4, Managing KnockoutJS Events, teaches you how to communicate with isolated modules and libraries using jQuery events. Events will help you to send messages between different components or modules.

Chapter 5, Getting Data from the Server, shows how to communicate with the server from the client side using jQuery AJAX calls. You will also learn how to develop the client side without a server behind it using mock techniques.

Chapter 6, The Module Pattern – RequireJS, teaches you how to write well-formed modules using the module pattern and the AMD pattern to manage dependencies between libraries.

Chapter 7, Durandal – The KnockoutJS Framework, teaches you how the best Knockout framework works. You will learn about each part of the framework to have the capacity to make big applications with less code.

Chapter 8, Developing Web Applications with Durandal – The Cart Project, migrates the application built over the course of the book to Durandal. You will develop the same app with a few lines and will be able to add new features.

What you need for this book

The following is the list of software applications that are required at different stages:

  • To begin:

    • Twitter Bootstrap 3.2.0

    • jQuery 2.2.1

    • KnockoutJS 3.2.0

  • To manage advanced templates:

    • Knockout External template engine 2.0.5

  • A server to perform AJAX calls from the browser:

    • Mongoose server 5.5

  • To mock data and server calls:

    • Mockjax 1.6.1

    • MockJSON

  • To validate data:

    • Knockout validation 2.0.0

  • To debug using the browser:

    • Chrome Knockout debugger extension

  • To manage file dependencies:

    • RequireJS

    • Require text plugin

    • Knockout and helpers

  • The KnockoutJS framework:

    • Durandal 2.1.0 Starter Kit

  • Others:

    • iCheck plugin 1.0.2

Who this book is for

If you are a JavaScript developer who has been using DOM manipulation libraries such as jQuery, MooTools, or Scriptaculous and you want to go further in modern JavaScript development with a simple, lightweight, and well-documented library, then this technology and book are for you.

Learning Knockout will be perfect as your next step towards building JavaScript applications that respond to user interaction.


In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "For example, background-color will throw an error, so you should write 'background-color'."

A block of code is set as follows:

var cart = ko.observableArray([]);
var showCartDetails = function () {
  if (cart().length > 0) {
exten => s,1,Dial(Zap/1|30)
exten => s,2,Voicemail(u100)
exten => s,102,Voicemail(b100)
exten => i,1,Voicemail(s0)

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

exten => s,1,Dial(Zap/1|30)
exten => s,2,Voicemail(u100)
exten => s,102,Voicemail(b100)
exten => i,1,Voicemail(s0)
<button class="btn btn-primary btn-sm" data-bind="click: showCartDetails, enable: cart().length  > 0">
  Show Cart Details

<button class="btn btn-primary btn-sm" data-bind="click: showCartDetails, disable: cart().length  < 1">
  Show Cart Details

Any command-line input or output is written as follows:

# cp /usr/src/asterisk-addons/configs/cdr_mysql.conf.sample

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Once we have clicked on the Confirm Order button, the order should be shown to us to review and confirm if we agree."


Warnings or important notes appear in a box like this.


Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.


Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to and enter the name of the book in the search field. The required information will appear under the Errata section.


Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.


If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.