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 4 USING URL ROUTINGurl attribute. Handling both events allows me to cater <strong>for</strong> the different kinds of input elements. I use thelive method, which is a neat jQuery feature that relies on event propagation to ensure that events arehandled <strong>for</strong> elements that are added to the document after the script has executed; this is essential whenthe set of elements in the document can be altered in response to view model changes. This approachallows me to use elements like this:......This markup generates a set of radio buttons <strong>for</strong> each element in the view model items array. Icreate the value <strong>for</strong> the data-url attribute with my custom <strong>for</strong>matAttr data binding, which I describedearlier. The select element requires some special handling because while the select element triggersthe change event, the in<strong>for</strong>mation about which value has been selected is derived from the child optionelements. Here is some markup that creates a select element that works with this pattern:......Part of the target URL is in the data-url attribute of the select element, and the rest is taken fromthe value attribute of the option elements. Some elements, including select, trigger both the click andchange events, so I check to see that the target URL differs from the current URL be<strong>for</strong>e usinglocation.replace to trigger a navigation change. Listing 4-10 shows how this technique can be appliedto select elements, buttons, radio buttons, and check boxes.Listing 4-10. Bridging Between Events and Routing <strong>for</strong> Different Kinds of ElementsRouting Example91www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!