Knockout.js
by Jamie Munro
Copyright 2015 Jamie Munro. All rights reserved.
Printed in the United States of America.
Published by OReilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
OReilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://safaribooksonline.com). For more information, contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com .
- Editor: Meg Foley
- Production Editor: Kara Ebrahim
- Copyeditor: Gillian McGarvey
- Proofreader: Marta Justak
- Indexer: Judy McConville
- Interior Designer: David Futato
- Cover Designer: Ellie Volckhausen
- Illustrator: Rebecca Demarest
- December 2014: First Edition
Revision History for the First Edition
- 2014-12-08: First Release
See http://oreilly.com/catalog/errata.csp?isbn=9781491914311 for release details.
The OReilly logo is a registered trademark of OReilly Media, Inc. Knockout.js, the cover image of a black tree kangaroo, and related trade dress are trademarks of OReilly Media, Inc.
While the publisher and the author have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the author disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights.
978-1-491-91431-1
[LSI]
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 timeno 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:
type=
'text/javascript'
src=
'js/knockout-3.2.0.js'
>
to:
type=
'text/javascript'
src=
'js/knockout-.js'
>
You will need to replace 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 -1. Installing KnockoutJS
Installing KnockoutJS
type=
'text/javascript'
src=
'js/knockout-3.2.0.js'
>
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:
ItalicIndicates 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 youre 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 OReilly 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 products 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 (OReilly). 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 worlds leading authors in technology and business.