Book Image

KnockoutJS Blueprints

By : Carlo Russo
Book Image

KnockoutJS Blueprints

By: Carlo Russo

Overview of this book

Table of Contents (12 chapters)
KnockoutJS Blueprints
About the Author
About the Reviewers

Communicating with other components

We realized the form, and now the customer can search for hotels; clicking on the search button will send a request to the backend, and we will use the result inside other components.

We need a way to enable communication between components without the need for direct referencing each other.

In the next chapter, we will look at the concept of Publish/Subscribe, but now we will go with a simple Event Manager module (following the Mediator design pattern).

When you got the files for the communication with the backend, you download BookingOnline/app/services/eventManager.js.

This module exposes three functions:

  • function on (event name, callback): use it to listen for named event

  • function off (event name, callback): use it to stop current listening for a named event

  • function trigger (event name, parameters): use it to send a message to the listeners

If you check the rest.js module, you will find there a function named getHotelByDestination. At the end of the function...