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
KnockoutJS Web Development
KnockoutJS Web Development

KnockoutJS Web Development: Efficiently work with data, web templates, and custom HTML tags using KnockoutJS

eBook
€8.99 €19.99
Paperback
€24.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

KnockoutJS Web Development

Chapter 2. Using Arrays, Nesting, and Grids

Now we've had a taste of KnockoutJS, we are now ready to learn new coding skills. In this chapter, we will see how to extend our MVVM skills to make a lot more sweet data interaction experiences. This chapter will focus on:

  • Conditional binding
  • Simpler nested binding
  • Observable arrays
  • Paged grids
  • Sorting data collections

Conditional binding

Data binding is the essence of many new libraries that interact with the HTML markup as it gets converted to the DOM. We saw binding in Chapter 1, Getting Started with KnockoutJS, using the data-bind ="..." bindings. Here we will be looking at conditional binding.

The first thing we will do is understand the concept of conditional binding. Our example is meant to be conceptual. We will show a more practical example shortly in this chapter.

The if binding plays a very similar role to the visible property in the DOM. The difference is that the if binding actually adds and removes the content from the DOM whereas visible swaps the CSS display style between visible and none.

Create a new file by copying the _base.htm file, located in the ko_2/do folder, and naming it condition.htm within the do folder. If you get stuck, there is a completed copy inside the done folder. To make things look better, we will now be using the larger Bootstrap template base as it will...

Introduction to arrays in Knockout

We will start by working with an unbound array. You will see that Knockout is smart enough to still work with the array and display the contents correctly. In fact, the array we will start out with will be an array with nested data. Add the highlighted data to the script section of our page:

myVM = {
showDetails: ko.observable(false),
employee: [
{name:"John Jones", spouse: { name: "Mary Jones"}},
{name:"Bill Williams", spouse: null},
{name:"Sandy Rivers", spouse: { name: "Mark Rivers"}},
]
}

The employee array data will automatically bind to the MVVM system when we run our applyBindings function on the page. We will need some markup to tell us that it has actually worked. I suggest separating the section of content on the page using the <hr/> tag just for clarity. Now add the following code to the markup section:

<ul data-bind="foreach: employee">
    <li>Employee: <strong data...

Sorting time

Displaying data is a very common use case for web pages. Perhaps the most common function people perform on data besides searching is sorting. We are going to look at how to sort data based on particular data fields. This time, we will create the logic first. Enter the following code into the script tag:

function doSort() {
    myVM.employee.sort(function (left, right) {
        return left.name == right.name ? 0 : (left.name < right.name ? -1 : 1);
    });
}

We will break down the logic for those unfamiliar with this level of JavaScript:

  1. The sort function passes in two structures. Each structure matches the items being sorted. The variable name could be anything; we choose left and right because it helps the programmer remember which variable is which. You can use, of course, any variable naming you choose. Each variable contains the whole structure of the item being passed in.
  2. The basic return value for sort needs to be true or false. This tells the program whether the two...

The simpleGrid plugin

Just like jQuery allows for custom plugins, it is possible to use plugins with Knockout as well. Here, we are going to use a simple plugin to add grid functionality to our page. This plugin also smartly adds paging to the page. We will also do some simple CSS to set our grid so it loosens up the space between the cells in the table.

Tip

The reason we code with jQuery, KnockoutJS, Bootstrap, and other Don't Repeat Yourself (DRY) libraries is that they package our work. We don't have to rethink, recode, or repeat our work when this is done. Using solutions such as jQuery and KnockoutJS, we can DRY out our code by adding our own library extensions. The simple grid plugin is an example of this.

Note that we will cover enough detail in this book that by the end you should be able to modify this plugin or build your own. However, you will have to understand JavaScript, CSS, HTML, and other topics enough to get the job you are seeking to DRY out. Regarding KnockoutJS...

Summary

If you have ever built any JavaScript pages with this much interaction with your data using JavaScript or even with jQuery, then you will know why the library deserves the title of Knockout. The amount of power we have gained using this library in just two chapters is compelling. Through this chapter you should have gained the ability to do conditional binding, nested binding, observable array collections, adding and deleting records in array style management, and sorting observable arrays. You even got a quick look at a Knockout plugin.

In the next chapter, we will learn to drive web forms with the power of Knockout. They think differently; and for a few, that is the first reaction. It is an interesting double take as the second reaction that nearly instantly follows is that it is better. Event binding is also a topic we will dig into in the next chapter with special focus on the over-the-top punch Knockout gives us when dealing with grid forms.

Left arrow icon Right arrow icon

Description

This book is for web developers and designers who work with HTML and JavaScript to help them manage data and interactivity with data using KnockoutJS. Knowledge about jQuery will be useful but is not necessary.

Who is this book for?

This book is for web developers and designers who work with HTML and JavaScript to help them manage data and interactivity with data using KnockoutJS. Knowledge about jQuery will be useful but is not necessary.

What you will learn

  • Understand the MVVM design is and how it can be made approachable
  • Simplify the usability of simple and detailed data collections
  • Work with web form fields including simpler ways to manage nested data collections
  • Manage your nested data collections efficiently, with web form fields
  • Use the alternative mapping style of coding with KnockoutJS mapping
  • Learn the joy of browserside templates to render KnockoutJS friendly code
  • Create your own custom HTML tags that provide dynamic interactive logic using meaningful markup naming conventions and structures
  • Simplify page logic with automated calculations and control the visibility of your CSS elements with the help of robust yet friendly templates

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Feb 27, 2015
Length: 178 pages
Edition : 1st
Language : English
ISBN-13 : 9781782161035
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Feb 27, 2015
Length: 178 pages
Edition : 1st
Language : English
ISBN-13 : 9781782161035
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 91.97
KnockoutJS Web Development
€24.99
MASTERING KNOCKOUTJS
€41.99
KNOCKOUTJS BLUEPRINTS
€24.99
Total 91.97 Stars icon
Banner background image

Table of Contents

7 Chapters
1. Getting Started with KnockoutJS Chevron down icon Chevron up icon
2. Using Arrays, Nesting, and Grids Chevron down icon Chevron up icon
3. Giving Forms the Knockout Touch Chevron down icon Chevron up icon
4. Coding – AJAX, Binding Properties, Mapping, and Utilities Chevron down icon Chevron up icon
5. The Joy of Templates Chevron down icon Chevron up icon
6. Packaged Elegance Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
(1 Ratings)
5 star 0%
4 star 100%
3 star 0%
2 star 0%
1 star 0%
Jrhd437 Sep 12, 2016
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
The book starts out pretty well, but I feel templates and components are glossed over too quickly. Still, it was a gentle introduction and, btw, Knockoutjs is still a lovely library and compatible with IE8.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.