zope.component.events: Event dispatching

The Component Architecture provides a way to dispatch events to event handlers using zope.event.notify(). Event handlers are registered as subscribers a.k.a. handlers.

Before we can start we need to import zope.component.event to make the dispatching effective:

Consider two event classes:

Now consider two handlers for these event classes:

We can register them with the Component Architecture:

Now let’s go through the events. We’ll see that the handlers have been called accordingly:

Object events

The objectEventNotify function is a subscriber to dispatch ObjectEvents to interested adapters.

First create an object class:

Then create an event class:

Create an object and an event:

Then notify the event to the subscribers. Since the subscribers list is empty, nothing happens.

Now create an handler for the event:

The event is notified to the subscriber:

Following test demonstrates how a subscriber can raise an exception to prevent an action.

Let’s create a container:

And put the object into the container:

Create an handler function that will raise an error when called:

Register the handler:

Then if we try to remove the object, an ObjectThrownEvent is fired: