O'Reilly logo

Knockout.js by Jamie Munro

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Preface

Dynamic Client-Side Applications

User Interfaces (UI) are becoming increasingly popular, much like responsive web design. Extremely popular websites like Gmail, Facebook, and Twitter have adopted extremely responsive client-side applications.

Rarely do you see a full-page refresh; instead, the content is loaded inline. Most importantly, your context is maintained and you are not forced to jump around between multiple pages.

A perfect example of this is Gmail. Ironically, it came from writing this book. I was reviewing an email from my editor and technical reviewer. I had the email opened and wanted to respond to it. Because Gmail placed my reply inline, I could easily review the email and write my response at the same time—no need to jump around or have two windows open.

KnockoutJS makes implementing web applications like this quick, efficient, and most importantly, easy to maintain!

Installing KnockoutJS

KnockoutJS made its debut back in July of 2010. It was released as an open source project by author Steve Sanderson. It is now being maintained by the open source community. Version 3.0 was released in late 2013.

It continues to evolves daily with new features, enhancements, and bug fixes. All of the examples in this book use version 3.2 (the stable release).

Setting up KnockoutJS is very simple. Begin by visiting the KnockoutJS downloads page.

You can download the minified version of Knockout by right-clicking (Ctrl+Click for Mac) and selecting the “Save link as...” option.

I suggest creating a new folder for all of the code examples in this book. Inside that folder, make a subfolder called js and place the knockout-3.2.0.js file inside.

Has the KnockoutJS Version Changed?

It is quite normal for popular open source frameworks to evolve quite quickly. If the current version of Knockout is not 3.2 when you download it, the examples in this book should continue to work. However, you will need to update the following:

<script type='text/javascript' 
src='js/knockout-3.2.0.js'></script>

to:

<script type='text/javascript' 
src='js/knockout-<current-version>.js'></script>

You will need to replace <current-version> with the correct value.

If you want to use version 3.2 like the examples in this book, you can visit the KnockoutJS project on Github and grab your version of choice.

Once the Knockout framework is downloaded, create an HTML page that loads the framework, like in Example P-1.

Example -1. Installing KnockoutJS
<!DOCTYPE html>
<html>
<head>
    <title>Installing KnockoutJS</title>
</head>
<body>
    <script type='text/javascript' src='js/knockout-3.2.0.js'></script>
</body>
</html>

In the example above, I included KnockoutJS just before the end body tag. This is done in order to allow the web browser to render the HTML without waiting for the JavaScript file to be downloaded and executed.

Some developers prefer to place the JavaScript inclusion inside the head tag. This is completely valid, especially in scenarios where you do not want the user to interact with your website prior to all JavaScript files being loaded and processed by the browser.

Conventions Used in This Book

The following typographical conventions are used in this book:

Italic

Indicates new terms, URLs, email addresses, filenames, and file extensions.

Constant width

Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, and keywords.

Constant width bold

Shows commands or other text that should be typed literally by the user.

Constant width italic

Shows text that should be replaced with user-supplied values or by values determined by context.

Tip

This element signifies a tip or suggestion.

Note

This element signifies a general note.

Warning

This element indicates a warning or caution.

Using Code Examples

Supplemental material (code examples, exercises, etc.) is available for download at https://github.com/oreillymedia/knockout_js.

This book is here to help you get your job done. In general, if example code is offered with this book, you may use it in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission.

We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “Knockout.js by Jamie Munro (O’Reilly). Copyright 2015 Jamie Munro, 978-1-491-91431-1.”

If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at .

Safari® Books Online

Safari Books Online is an on-demand digital library that delivers expert content in both book and video form from the world’s leading authors in technology and business.

Technology professionals, software developers, web designers, and business and creative professionals use Safari Books Online as their primary resource for research, problem solving, learning, and certification training.

Safari Books Online offers a range of plans and pricing for enterprise, government, education, and individuals.

Members have access to thousands of books, training videos, and prepublication manuscripts in one fully searchable database from publishers like O’Reilly Media, Prentice Hall Professional, Addison-Wesley Professional, Microsoft Press, Sams, Que, Peachpit Press, Focal Press, Cisco Press, John Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe Press, FT Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, Course Technology, and hundreds more. For more information about Safari Books Online, please visit us online.

How to Contact Us

Please address comments and questions concerning this book to the publisher:

  • O’Reilly Media, Inc.
  • 1005 Gravenstein Highway North
  • Sebastopol, CA 95472
  • 800-998-9938 (in the United States or Canada)
  • 707-829-0515 (international or local)
  • 707-829-0104 (fax)

We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at http://bit.ly/knockoutjs-book.

To comment or ask technical questions about this book, send email to .

For more information about our books, courses, conferences, and news, see our website at http://www.oreilly.com.

Find us on Facebook: http://facebook.com/oreilly

Follow us on Twitter: http://twitter.com/oreillymedia

Watch us on YouTube: http://www.youtube.com/oreillymedia

Acknowledgments

Where do I begin? I thought that writing my fourth book would be a lot easier than it was. My last book came together so smoothly, and I thought for sure this would be the same. It definitely was not! At every stage of this book, I was forced to stop and think “Why am I doing this a particular way?” There are so many things I do with Knockout and JavaScript a certain way without thinking, that I was forced to think about the reasons I do it this way. It was such a huge personal growth experience and I feel like a much better developer and hopefully these lessons will be passed on to each and every one of you.

I must first thank my wife, Shannon. She helped immensely by being such a good mom to our three young children—Lily, Owen, and Kayla—while I was writing this book. Without her love and support, I would get no sleep at all!

Secondly, to Steve Kennedy (you may see his name pop up every now and then in examples). We spent many a lunch hour going over minute details of almost every aspect of the book. He made sure that every example was technically accurate and ensured the “why” was thoroughly covered. I hope the “why” things are done will be invaluable throughout this book.

Third, to Mike Wilson (best to not try the email address in Example 9-4!). Mike really helped shape the early chapters to ensure we got right to the examples before presenting the theory. As a reader of many books, this always makes me feel better to see the results of the “Hello World” example immediately.

I can’t forget the team at O’Reilly. Thanks to Meg, Kara, and Gillian for making this a wonderful experience.

And finally, to you, the reader: I hope you enjoy reading this book and learn as much as I did from writing it!

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required