Book Image

KnockoutJS by Example

By : Adnan Jaswal
Book Image

KnockoutJS by Example

By: Adnan Jaswal

Overview of this book

KnockoutJS By Example is a project-based guide that introduces the key features and concepts of knockout.js. It helps you create an application skeleton and a Hello World application. You will develop a To-Do list application that aims to show the basic features of knockout.js in action, such as data binding and observables, following which you will develop a dynamic online customer registration form that captures and validates customer information. This book will further walk you through developing a customer banking portal, which demonstrates the use of knockout.js with components such as navigation bars, tabs, carousels, master details view, panels, forms, and wizards. You will also discover how to use token-based authentication and authorization to secure the customer banking portal, and move on to creating an editable products grid with CRUD operations. Finally, you will explore how to use the Google Maps API with knockout.js. KnockoutJS By Example will not only leave you with a basic understanding of knockout.js fundamentals but also take you through some of the advanced features. It will help you get a web application up and ready instantly.
Table of Contents (17 chapters)
KnockoutJS by Example
Credits
About the Author
Acknowledgment
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

JavaScript technologies are playing a much larger role in modern web applications. These application are expected to be rich, interactive, responsive, modular, and maintainable. The applications are often required to redraw parts of the user interface. The data and business logic must be kept separate from the presentation in order to develop modular and maintainable web applications that are interactive and provide a richer user experience. The data and the presentation could then be bound in a way that updates to one would update the other. Similarly, the business logic could be bound to events triggered by the presentation. Developing applications on this design in pure JavaScript can be complex and time-consuming. It becomes evident, to most developers, that a library is required that allows the development of modern web applications without getting into the complexities of binding data, business logic, and presentation. Knockout is one such library.

Knockout is an open source JavaScript library. It reduces the complexities of JavaScript and HTML development by following the stated design principle and implementing the Model-View-View Model (MVVM) design pattern.

One of the best ways to learn a software development technology is by example. Keeping this in mind, my intent has been to provide a practical and hands on learning experience featuring real-world projects. I have drawn on my experience as a software designer and developer to provide you with a practical guide. The inspiration for the content and examples in this book come from my years of experience in developing web applications using JavaScript and, in particular, developing applications using Knockout for a leading financial institution.

If you are new to Knockout, this book is a hands-on guide for you to start creating web applications. With its iterative approach, sample code, and screenshots, this book will take you on a journey of discovering the power of Knockout.

If you are an experienced Knockout developer, this book will give you practical solutions to real-world problems. With advanced topics such as building complex navigations, securing web applications, building services for CRUD operations, and using third party APIs, this book will be your go-to reference.

What this book covers

Chapter 1, Getting Started, covers the basic concepts and patterns that help us understand how Knockout works. It explores the key features of Knockout, including declarative binding, automatic UI refresh, dependency tracking, and templating. The second half of the chapter takes you through building your first Knockout application—an address book.

Chapter 2, Creating a To-do List Application, takes you through building a to-do list application. The application's features include adding, viewing, deleting, sorting, and completing tasks. It also includes features to set the priority on tasks and view the number of total and completed tasks.

Chapter 3, Creating an Online Customer Registration Form, walks you through building a customer registration form. The information captured by the form includes personal information, contact details, residential and postal addresses, and credit card information. The application demonstrates Knockout's ability to create dynamic forms.

Chapter 4, Adding Validation to the Customer Registration Form, describes how to add validation to the form that we built in the previous chapter. It explores two ways of applying validation: using custom extenders and the Knockout validation plugin.

Chapter 5, Creating a Customer Banking Portal, is the first chapter in a series of three that walks you through building a customer banking portal for MyBank. It lets you set up the navigation for the application, display users' contact details, their accounts and associated transactions, and their personal information as well.

Chapter 6, Enhancing the Customer Banking Portal, adds new features to the customer banking portal that we built in the previous chapter. This chapter helps you add features to allow the users to update their personal information and transfer funds between their accounts using a wizard component.

Chapter 7, Securing the Customer Banking Portal, explores the common token-based authentication mechanisms used in modern web applications. It walks you through securing the customer banking portal using token-based authentication.

Chapter 8, Building an Editable Products Grid with CRUD Operations, walks you through building an editable products grid application. The application integrates with a server through RESTful web services. Its features include displaying, deleting, adding, and updating products.

Chapter 9, Using Google Maps APIs with Knockout, walks you through building a map application using the Google Maps APIs. The application gives the users the ability to enter address information with autocomplete predictions, based on partial address input, and displays detailed address information. It also renders a map, places markers based on the addresses selected, and displays the route between the two markers.

What you need for this book

In order to start coding along the examples and running the sample applications that come with this book, you need a device with internet connectivity so that you can download the libraries used in the examples as well as the sample code. You can use your preferred editor or development environment to write the code and start developing web applications. You also require one of the major browsers to run the applications.

Who this book is for

This book is intended for designers and developers who want to learn how to use the Knockout library to develop rich, interactive, and modular web applications. This book will help you master both the basic and the advanced features of Knockout. The book assumes no prior knowledge of the Knockout library, but basic familiarity with HTML, CSS, and JavaScript would be helpful. The book is targeted towards readers who are inspired by the idea of hands-on learning. The sample real-world applications in this book will take you on a journey of building applications that range from basic level all the way to advance.

Conventions

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: "The retrieveContact function, which is used to retrieve a contact from the server-side."

A block of code is set as follows:

var contact = {
    id: 1,
    name: 'John',
    phoneNumber: '00 11 000000'
};

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

/* the model */
var member = {
    accounts: ko.observableArray(),
    selectedAccount: ko.observable(),
    selectedAccountTransactions: ko.observableArray([])
};

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: "Open the application in the browser and try hitting Submit without entering any information in the form fields."

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 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 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.

License

All code examples, whether in the book or downloaded from your account at http://www.packtpub.com are released under the terms of MIT license as stated here:

Copyright (c) Adnan Jaswal, 2015

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. 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.

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 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 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 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 https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

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.

Questions

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.