Skip to content

Commit

Permalink
[#1377] Add best practice about not overusing pubsub
Browse files Browse the repository at this point in the history
  • Loading branch information
Sean Hammond committed Dec 17, 2013
1 parent 4c50edc commit 508ce97
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
15 changes: 15 additions & 0 deletions doc/theming/best-practices.rst
Expand Up @@ -97,6 +97,21 @@ should have a ``teardown()`` function that calls
CKAN calls the ``teardown()`` functions of modules when those modules are
removed from the page.

.. _pubsub overuse best practice:

--------------------
Don't overuse pubsub
--------------------

There shouldn't be very many cases where a JavaScript module really needs to
use :ref:`Pubsub <pubsub>`, try to only use it when you really need to.

JavaScript modules in CKAN are designed to be small and loosely-coupled,
for example modules don't share any global variables and don't call
each other's functions. But pubsub offers a way to tightly couple JavaScript
modules together, by making modules depend on multiple events published by
other modules. This can make the code buggy and difficult to understand.


--------------------------------------------
Use ``{% snippet %}``, not ``{% include %}``
Expand Down
7 changes: 7 additions & 0 deletions doc/theming/javascript.rst
Expand Up @@ -264,6 +264,8 @@ Error handling
Add an example of how to handle error responses when making ajax requests.


.. _pubsub:

------
Pubsub
------
Expand Down Expand Up @@ -306,6 +308,11 @@ The way it works is:
``teardown()`` functions of modules when those modules are removed from the
page. See :ref:`pubsub unsubscribe best practice`.

.. warning::

Don't tightly couple your JavaScript modules by overusing pubsub.
See :ref:`pubsub overuse best practice`.

Remember that because we attach our ``example_theme_popover.js`` module to a
``<button>`` element that is rendered once for each dataset on the page, CKAN
creates one instance of our module for each dataset. The only way these objects
Expand Down

0 comments on commit 508ce97

Please sign in to comment.