Book Image

Mastering KnockoutJS

By : Timothy Moran
Book Image

Mastering KnockoutJS

By: Timothy Moran

Overview of this book

Table of Contents (16 chapters)
Mastering KnockoutJS
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

Knockout is built around a pattern that started in Microsoft. This model is Model-View-ViewModel (MVVM), and I think introducing this pattern to newcomers is one of the biggest obstacles to wider adoption. Nearly every other JavaScript library or framework, along with most server-side frameworks, has been built around the Model-View-Controller (MVC) pattern, and the differences between the two are sometimes confusing, even for experienced developers. This problem is compounded by the fact that some larger frameworks, such as AngularJS, end up with a pattern that is nearly identical to MVVM.

Knockout's documentation is excellent, and its live examples and interactive tutorials are some of the best. When it comes to organizing full applications, though, more explanation is required. When I started writing this book, there was only one book on Knockout on Amazon, and it didn't have very favorable reviews. It seemed like a complete guide to using Knockout as the central piece of a frontend stack was missing.

I have been using Knockout for 3 years now, and I've been an active member of the community on StackOverflow and GitHub for 2 years. I've used Knockout in several professional applications as well as a dozen or so personal projects. It is, by far, my favorite JavaScript library, and I strongly prefer MVVM over MVC for developing client applications. Hopefully, you feel that this book gives you everything you need in order to be successful with Knockout.

A note on Knockout 3.2

Knockout 3.2 was released while this book was being written. Chapter 4, Application Development with Components and Modules, was rewritten to include the components feature, and some minor changes were made to other chapters in order to make them accurate. However, most of the code samples were written against Knockout 3.1, and so they do not take advantage of pure computed observables or other features that were released in Knockout 3.2.

What this book covers

Chapter 1, Knockout Essentials, covers the environment setup and basic use of the Knockout library. It also covers data binding, observables, binding handlers, and extenders, and demonstrates a simple Knockout Contacts List application.

Chapter 2, Extending Knockout with Custom Binding Handlers, gives you in-depth knowledge of how to create and use custom binding handlers. It includes simple single-property binding handlers as well as complex multiproperty binding handlers with templates.

Chapter 3, Extending Knockout with Preprocessors and Providers, teaches you how to use node and binding preprocessors and binding providers to customize Knockout's syntax. It also explores the Knockout Punches library.

Chapter 4, Application Development with Components and Modules, explains how to use RequireJS Asynchronous Module Definitions (AMDs) with Knockout to create organized, modular viewmodels. It also teaches you how to use the new Knockout components feature and how to continue working with the Contacts List demo application.

Chapter 5, Durandal – the Knockout Framework, explores the basics of the Knockout-based Durandal framework. This chapter covers composition, routing, modal dialogs, and custom widgets.

Chapter 6, Advanced Durandal, continues looking at the use of the Durandal framework. This chapter covers events, advanced composition, nested routers, custom dialogs, and the observable plugin.

Chapter 7, Best Practices, takes a deep dive into the inner workings of Knockout. It includes dependency detection and the publish/subscribe implementation, observable inheritance, the template engine, and a complete Knockout utility (ko.utils) reference.

Chapter 8, Plugins and Other Knockout Libraries, gives you an overview of the recommended patterns and practices for Knockout developers.

Chapter 9, Under the Hood, covers several popular Knockout plugins, including Knockout validation, Knockout mapping, and the new Knockout-ES5 plugin.

What you need for this book

You need an ES5-compatible browser, Git, and Node.js. The code in this book will run on any operating system.

Who this book is for

If you are an experienced JavaScript developer who is looking for new tools to build web applications and gain an understanding of core elements and applications, this is the book for you. It is assumed that you have basic knowledge of DOM, JavaScript, and KnockoutJS.

Conventions

In this book, you will find a number of styles of text 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: "All the code needed to start each chapter can be found in a branch named cp[chapter#]-[sample]."

A block of code is set as follows:

var subtotal = ko.observable(0);
var tax = ko.observable(0.05);
var total = ko.computed(function() {
  var subtotal = parseFloat(self.subtotal()),
  tax = parseFloat(self.tax());
  return subtotal * (1 + tax);
});

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "You might have noticed in the previous section that when the Contacts page view model communicated with the data service, it wasn't dealing with JSON, but real JavaScript objects."

Note

Warnings or important notes appear in a box like this.

Tip

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 may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via 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 on www.packtpub.com/authors.

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 for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Additionally, the code for this book is part of a Git repository, which is available on GitHub at https://github.com/tyrsius/MasteringKnockout. The code samples are organized as branches in the repository.

Errata

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 would 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 http://www.packtpub.com/submit-errata, 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 on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright 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.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.