Table Of Contents

Previous topic

21.32. The NetworkLatencySimulator Object

Next topic

21.34. The PhysicsManager Object

This Page

21.33. The Observer Object

A utility object that implements the observer design pattern. It maintains a list of functions to call when an event occurs.

Required scripts

The Observer object requires:

/*{{ javascript("jslib/observer.js") }}*/

21.33.1. Constructor

21.33.1.1. create

Summary

Creates and returns an Observer object.

Syntax

var observer = Observer.create();

21.33.2. Methods

21.33.2.1. subscribe

Summary

Add a function to the list of functions to call when the event occurs. The function can only be registered once.

The functions should take a single argument, i.e. callback(data);

Using closure is useful pattern to allow additional state to be associated with the function.

Syntax

observer.subscribe(callback);
callback
A function to call.

21.33.2.2. unsubscribe

Summary

Remove a previously registered function.

Syntax

observer.unsubscribe(callback);
callback
A function.

21.33.2.3. unsubscribeAll

Summary

Remove all previously registered functions.

Syntax

observer.unsubscribeAll();

21.33.2.4. notify

Summary

Calls all the subscribed functions with the passed in data.

Syntax

observer.notify(data);
data
An object that is passed as an argument to the function.