12.07.2015 Views

Pro JavaScript for Web Apps pdf - EBook Free Download

Pro JavaScript for Web Apps pdf - EBook Free Download

Pro JavaScript for Web Apps pdf - EBook Free Download

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 3 ADDING A VIEW MODELThe nice thing about this feature is that KO manages all of the dependencies, such that when mycomputed observable function relies on a regular observable data item, a change to the regular itemautomatically triggers an update in the computed value. I’ll use this behavior to manage the overall totallater in this chapter.Next, I need to add some elements with bindings to the template, as shown in Listing 3-8.Listing 3-8. Adding Elements to the Template to Support Subtotals $()($)The inner span element uses a text data binding to display the value of the subtotal property Icreated a moment ago. To make things more interesting, the outer span element uses another KObinding; this one is visible. For this binding, the child elements are hidden when the specified propertyis false-like (zero, null, undefined, or false). For truth-like values (1, true, or a non-null object or array),the child elements are displayed. I have specified the subtotal value <strong>for</strong> the visible binding, and thislittle trick means that I will display a subtotal only when the user enters a nonzero value into the inputelement. You can see the result in Figure 3-4.Figure 3-4. Selectively displaying subtotals65www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!