Pro JavaScript for Web Apps – crossroads

Apress has an awesome book that covers KnockoutJS: Pro JavaScript for Web Apps.

You will get stuck on Chapter 4, when using the latest latest version of crossroads, hasher and signals because setting the context of hasher to be crossroads doesn’t seem to work anymore:

     hasher.initialized.add(crossroads.parse, crossroads);
     hasher.changed.add(crossroads.parse, crossroads);
     hasher.init();

     crossroads.addRoute("select/{item}", function (item) {
           viewModel.selectedItem(item);
     });

The code works with the version of libraries included in the book’s downloadable source code, but if you do a latest git pull of all 3 libraries you need to change the code as per the example as given in the Hasher GitHub readme

crossroads.addRoute("select/{item}", function (item) {
  viewModel.selectedItem(item);
});
function handleChanges(newHash, oldHash) {
  crossroads.parse(newHash);
};
hasher.changed.add(handleChanges);
hasher.initialized.add(handleChanges);
hasher.init();

Also, the crossroads routes must be set up before the hasher.init(). Otherwize the hasher initialized callback will have no routes to execute.
Otherwise, the book is great! Bunch of people in the office want to borrow it once I am done with it:)

Leave a Reply

Your email address will not be published. Required fields are marked *