<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="X-UA-Compatible" content="IE=Edge" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Events — zope.component 4.4.1 documentation</title> <link rel="stylesheet" href="_static/alabaster.css" type="text/css" /> <link rel="stylesheet" href="_static/pygments.css" type="text/css" /> <script type="text/javascript" id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script> <script type="text/javascript" src="_static/jquery.js"></script> <script type="text/javascript" src="_static/underscore.js"></script> <script type="text/javascript" src="_static/doctools.js"></script> <script type="text/javascript" src="_static/language_data.js"></script> <link rel="index" title="Index" href="genindex.html" /> <link rel="search" title="Search" href="search.html" /> <link rel="next" title="Factories" href="factory.html" /> <link rel="prev" title="The Zope 3 Component Architecture (Socket Example)" href="socketexample.html" /> <link rel="stylesheet" href="_static/custom.css" type="text/css" /> <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" /> </head><body> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body" role="main"> <div class="section" id="events"> <h1>Events<a class="headerlink" href="#events" title="Permalink to this headline">¶</a></h1> <p>The Component Architecture provides a way to dispatch events to event handlers. Event handlers are registered as <em>subscribers</em> a.k.a. <em>handlers</em>.</p> <p>Before we can start we need to import <code class="docutils literal notranslate"><span class="pre">zope.component.event</span></code> to make the dispatching effective:</p> <p>Consider two event classes:</p> <p>Now consider two handlers for these event classes:</p> <p>We can register them with the Component Architecture:</p> <p>Now let’s go through the events. We’ll see that the handlers have been called accordingly:</p> <div class="section" id="object-events"> <h2>Object events<a class="headerlink" href="#object-events" title="Permalink to this headline">¶</a></h2> <p>The <code class="docutils literal notranslate"><span class="pre">objectEventNotify</span></code> function is a subscriber to dispatch ObjectEvents to interested adapters.</p> <p>First create an object class:</p> <p>Then create an event class:</p> <p>Create an object and an event:</p> <p>Then notify the event to the subscribers. Since the subscribers list is empty, nothing happens.</p> <p>Now create an handler for the event:</p> <p>The event is notified to the subscriber:</p> <p>Following test demonstrates how a subscriber can raise an exception to prevent an action.</p> <p>Let’s create a container:</p> <p>And put the object into the container:</p> <p>Create an handler function that will raise an error when called:</p> <p>Register the handler:</p> <p>Then if we try to remove the object, an ObjectThrownEvent is fired:</p> </div> </div> </div> </div> </div> <div class="sphinxsidebar" role="navigation" aria-label="main navigation"> <div class="sphinxsidebarwrapper"> <h1 class="logo"><a href="index.html">zope.component</a></h1> <h3>Navigation</h3> <ul class="current"> <li class="toctree-l1"><a class="reference internal" href="narr.html">Zope Component Architecture</a></li> <li class="toctree-l1"><a class="reference internal" href="socketexample.html">The Zope 3 Component Architecture (Socket Example)</a></li> <li class="toctree-l1 current"><a class="current reference internal" href="#">Events</a><ul> <li class="toctree-l2"><a class="reference internal" href="#object-events">Object events</a></li> </ul> </li> <li class="toctree-l1"><a class="reference internal" href="factory.html">Factories</a></li> <li class="toctree-l1"><a class="reference internal" href="persistentregistry.html">Persistent Component Management</a></li> <li class="toctree-l1"><a class="reference internal" href="zcml.html">ZCML directives</a></li> <li class="toctree-l1"><a class="reference internal" href="configure.html">Package configuration</a></li> <li class="toctree-l1"><a class="reference internal" href="hooks.html">The current component registry</a></li> <li class="toctree-l1"><a class="reference internal" href="testlayer.html">Layers</a></li> <li class="toctree-l1"><a class="reference internal" href="api.html"><code class="docutils literal notranslate"><span class="pre">zope.component</span></code> API Reference</a></li> <li class="toctree-l1"><a class="reference internal" href="hacking.html">Hacking on <code class="docutils literal notranslate"><span class="pre">zope.component</span></code></a></li> </ul> <div class="relations"> <h3>Related Topics</h3> <ul> <li><a href="index.html">Documentation overview</a><ul> <li>Previous: <a href="socketexample.html" title="previous chapter">The Zope 3 Component Architecture (Socket Example)</a></li> <li>Next: <a href="factory.html" title="next chapter">Factories</a></li> </ul></li> </ul> </div> <div id="searchbox" style="display: none" role="search"> <h3>Quick search</h3> <div class="searchformwrapper"> <form class="search" action="search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> </div> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="footer"> ©Zope Foundation and Contributors. | Powered by <a href="http://sphinx-doc.org/">Sphinx 1.8.3</a> & <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.8</a> | <a href="_sources/event.rst.txt" rel="nofollow">Page source</a> </div> </body> </html>