Book Image

Mastering KnockoutJS

By : Timothy Moran
Book Image

Mastering KnockoutJS

By: Timothy Moran

Overview of this book

Table of Contents (16 chapters)
Mastering KnockoutJS
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Publishing and subscribing


A very common problem new developers face when they start using Knockout is how to communicate between viewmodels without establishing a single hierarchy with a master viewmodel or any other form of direct reference between viewmodel objects. These kind of hard dependencies are generally considered bad practice, but the need to send messages between different viewmodels is unavoidable.

The publish-subscribe (pub/sub) pattern is a popular solution to this problem. Durandal offers you a simple pub/sub implementation via the Events module. There are two ways in which you can use the events system: with the events included on the durandal/app object by default or by adding events to your own objects.

The events module

The events system includes the events module and the subscription class. The events module, required by durandal/events, provides you with the includeIn method to add events to an object. When Events.includeIn(obj) is called, the following functions are...