summaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorChris McDonough <chrism@plope.com>2013-08-13 06:35:17 -0400
committerChris McDonough <chrism@plope.com>2013-08-13 06:35:17 -0400
commit0d37d81949bc82de1bd9c1193f282909f9576c3a (patch)
tree6aaacaeb083c206a2726d406addadef1f6a4b257 /docs
parent986dc5f96c3bf0a62eb8984bfa5c2da4a3e2f254 (diff)
parent5ae91a8ef06c4484bf748c7be578b28d0ca0f12c (diff)
downloadpyramid-0d37d81949bc82de1bd9c1193f282909f9576c3a.tar.gz
pyramid-0d37d81949bc82de1bd9c1193f282909f9576c3a.tar.bz2
pyramid-0d37d81949bc82de1bd9c1193f282909f9576c3a.zip
Merge branch 'master' of github.com:Pylons/pyramid
Diffstat (limited to 'docs')
m---------docs/_themes0
-rw-r--r--docs/api/registry.rst9
-rw-r--r--docs/narr/events.rst89
-rw-r--r--docs/narr/templates.rst2
4 files changed, 96 insertions, 4 deletions
diff --git a/docs/_themes b/docs/_themes
-Subproject f59f7bfce5259f50fbb67b9040c03ecb080130b
+Subproject 8673c4a14f192c15f1949dc9862821e60f31604
diff --git a/docs/api/registry.rst b/docs/api/registry.rst
index db348495c..7736cf075 100644
--- a/docs/api/registry.rst
+++ b/docs/api/registry.rst
@@ -29,6 +29,15 @@
This attribute is often accessed as ``request.registry.introspector`` in
a typical Pyramid application.
+ .. method:: notify(*events)
+
+ Fire one or more events. All event subscribers to the event(s)
+ will be notified. The subscribers will be called synchronously.
+ This method is often accessed as ``request.registry.notify``
+ in Pyramid applications to fire custom events. See
+ :ref:`custom_events` for more information.
+
+
.. class:: Introspectable
.. versionadded:: 1.3
diff --git a/docs/narr/events.rst b/docs/narr/events.rst
index 929208083..11af89ca6 100644
--- a/docs/narr/events.rst
+++ b/docs/narr/events.rst
@@ -53,7 +53,7 @@ method (see also :term:`Configurator`):
from subscribers import mysubscriber
- # "config" below is assumed to be an instance of a
+ # "config" below is assumed to be an instance of a
# pyramid.config.Configurator object
config.add_subscriber(mysubscriber, NewRequest)
@@ -77,7 +77,7 @@ type via the :func:`pyramid.events.subscriber` function.
@subscriber(NewRequest)
def mysubscriber(event):
- event.request.foo = 1
+ event.request.foo = 1
When the :func:`~pyramid.events.subscriber` decorator is used a
:term:`scan` must be performed against the package containing the
@@ -113,7 +113,7 @@ your application like so:
:linenos:
def handle_new_request(event):
- print 'request', event.request
+ print 'request', event.request
def handle_new_response(event):
print 'response', event.response
@@ -150,3 +150,86 @@ application, because the interface defined at
:class:`pyramid.interfaces.INewResponse` says it must
(:class:`pyramid.events.NewResponse` objects also have a ``request``).
+.. _custom_events:
+
+Creating Your Own Events
+------------------------
+
+In addition to using the events that the Pyramid framework creates,
+you can create your own events for use in your application. This can
+be useful to decouple parts of your application.
+
+For example, suppose your application has to do many things when a new
+document is created. Rather than putting all this logic in the view
+that creates the document, you can create the document in your view
+and then fire a custom event. Subscribers to the custom event can take
+other actions, such as indexing the document, sending email, or
+sending a message to a remote system.
+
+An event is simply an object. There are no required attributes or
+method for your custom events. In general, your events should keep
+track of the information that subscribers will need. Here are some
+example custom event classes:
+
+.. code-block:: python
+ :linenos:
+
+ class DocCreated(object):
+ def __init__(self, doc, request):
+ self.doc = doc
+ self.request = request
+
+ class UserEvent(object):
+ def __init__(self, user):
+ self.user = user
+
+ class UserLoggedIn(UserEvent):
+ pass
+
+Some Pyramid applications choose to define custom events classes in an
+``events`` module.
+
+You can subscribe to custom events in the same way that you subscribe
+to Pyramid events -- either imperatively or with a decorator. You can
+also use custom events with :ref:`subscriber predicates
+<subscriber_predicates>`. Here's an example of subscribing to a custom
+event with a decorator:
+
+.. code-block:: python
+ :linenos:
+
+ from pyramid.events import subscriber
+ from .events import DocCreated
+ from .index import index_doc
+
+ @subscriber(DocCreated)
+ def index_doc(event):
+ # index the document using our application's index_doc function
+ index_doc(event.doc, event.request)
+
+The above example assumes that the application defines a
+``DocCreated`` event class and an ``index_doc`` function.
+
+To fire your custom events use the
+:meth:`pyramid.registry.Registry.notify` method, which is most often
+accessed as ``request.registry.notify``. For example:
+
+.. code-block:: python
+ :linenos:
+
+ from .events import DocCreated
+
+ def new_doc_view(request):
+ doc = MyDoc()
+ event = DocCreated(doc, request)
+ request.registry.notify(event)
+ return {'document': doc}
+
+This example view will notify all subscribers to the custom
+``DocCreated`` event.
+
+Note that when you fire an event, all subscribers are run
+synchronously so it's generally not a good idea
+to create event handlers that may take a long time to run. Although
+event handlers could be used as a central place to spawn tasks on your
+own message queues.
diff --git a/docs/narr/templates.rst b/docs/narr/templates.rst
index d4cf20b93..af5ad6c5c 100644
--- a/docs/narr/templates.rst
+++ b/docs/narr/templates.rst
@@ -723,7 +723,7 @@ This template doesn't use any advanced features of Mako, only the
Using A Mako def name Within a Renderer Name
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-Sommetime you'd like to render a ``def`` inside of a Mako template instead of
+Sometimes you'd like to render a ``def`` inside of a Mako template instead of
the full Mako template. To render a def inside a Mako template, given a
:term:`Mako` template file named ``foo.mak`` and a def named ``bar``, you can
configure the template as a :term:`renderer` like so: