Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Knockout.JS Essentials

You're reading from   Knockout.JS Essentials Implement a successful JavaScript-rich application with KnockoutJS, jQuery, and Bootstrap

Arrow left icon
Product type Paperback
Published in Feb 2015
Publisher
ISBN-13 9781784397074
Length 232 pages
Edition 1st Edition
Languages
Arrow right icon
Toc

Table of Contents (10) Chapters Close

Preface 1. Refreshing the UI Automatically with KnockoutJS FREE CHAPTER 2. KnockoutJS Templates 3. Custom Bindings and Components 4. Managing KnockoutJS Events 5. Getting Data from the Server 6. The Module Pattern – RequireJS 7. Durandal – The KnockoutJS Framework 8. Developing Web Applications with Durandal – The Cart Project Index

Summary

In this chapter, you have learned the basics of the Knockout library. We have created a simple form to add products to our catalog. You have also learned how to manage observable collections and display them in a table. Finally, we have developed the search functionality using computed observables.

You have learned three important Knockout concepts:

  • View-model: This holds the data that represents the state of the view. It is a pure JavaScript object.
  • Models: This contains data from the business domain.
  • Views: This displays the data we store in the view-model in a given instant of time.

To build reactive UIs, the Knockout library provides us with some important methods:

  • ko.observable: This is used to manage variables.
  • ko.observableArray: This is used to manage arrays.
  • ko.computed: They respond to changes from observables that are inside them.

To iterate over the elements of an array, we use the foreach binding. When we use the foreach binding, we create a new context. This context is relative to each item. If we want to access out of this context we should use the $parent object.

When we want to create a new context relative to a variable, we can attach the with binding to any DOM element.

We use the click binding to attach the click event to an element. Click on event functions to always get the context as the first parameter.

To get values from a variable that we are not sure is an observable, we can use the ko.unwrap function.

We can use the ko.utils.arrayFilter function to filter collections.

In the next chapter, we are going to use templates to keep our code maintainable and clean. Template engines help us to keep our code arranged and allow us to update views in an easy way.

There is a copy of the code developed in this chapter at:

https://github.com/jorgeferrando/knockout-cart/archive/chapter1.zip.

You have been reading a chapter from
Knockout.JS Essentials
Published in: Feb 2015
Publisher:
ISBN-13: 9781784397074
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image