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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 4 USING URL ROUTINGwe require from the download archives, which should be copied into the Node.js server contentdirectory. (All three files are part of the source code download <strong>for</strong> this book if you don’t want todownload these files individually. The download is freely available at Apress.com.)Table 4-1. Crossroads <strong>JavaScript</strong> LibrariesLibrary Name URL Required FileCrossroadsSignalsHasherhttp://millermedeiros.github.com/crossroads.js/ crossroads.jshttp://millermedeiros.github.com/js-signals/ signals.jshttps://github.com/millermedeiros/hasher/ hasher.jsI added Crossroads, its supporting libraries, and my new cheeseutils.js file into the HTMLdocument using script elements:......Adding the View Model and Content MarkupURL routing works extremely well when combined with a view model in a web application. For thisinitial application, I have created a very simple view model, as follows:var viewModel = {items: ["Apple", "Orange", "Banana"],selectedItem: ko.observable("Apple")};There are two properties in the view model. The items property refers to an array of three strings.The selectedItem property is an observable data item that keeps track of which item is presentlyselected. I use these values with data bindings to generate the content in the document, like this:...The selected item is: ...The bindings that KO supports by default are pretty basic, but it is easy to create custom ones, whichis exactly what I have done <strong>for</strong> the fadeVisible binding referred to in the listing. Listing 4-2 shows the79www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!