Using the Arbiter.js Library
As I have been working toward the newest version of Page List+, one of my most popular concrete5 addons, I realized that it was going to be a much bigger job than I initially thought. The reason in one word: Ajax.
In this new version, you'll be able to setup your Page List+ blocks to refresh without reloading the page. That, in and of itself, wasn't the issue. The issue was compatibility with another addon that I provide, Infinite Scroller. You see, these two addons must be closely coupled in order to work, but some of the fixes that I came up with for Infinite Scroller were actually in the Page List+ code. Very bad design.
I needed to decouple the code from one addon and place it in another. One of my past colleague's projects, Arbiter.js, came up in my research on the subject. You can think of it in two different ways:
- One piece of the page can 'publish' a message to any other part of the page can 'listen' to.
- One piece of the page can 'trigger' an event that another part of the page can respond to.
A Real World Example of how Arbiter.js Works
In the new versions of these addons, I'm using Arbiter.js like this:
Setup by the Site Owner
- The site owner creates two Page List+ blocks. One only uses the 'form' functionality. The other uses only the 'results' functionality. (This is a really nice way of 'decoupling' the functionality of the form and a custom results template.)
- The first block is setup to 'submit via ajax' and the second is set to 'receive via ajax'.
Used by the Site Visitor
- The visitor changes the values in the form and submits it
- Behind the scenes, this is what happens:
- The Page List+ 'form' block publishes a message that the form was submitted and sends along the form data
- The Page List+ 'results' block is listening to those messages, so it gets the form data and sends it back to the server to get a list of corresponding pages.
- When the 'results' block gets the list of pages, it displays them. Then it, in turn, publishes a message that the results are now shown.
- The Infinite Scroller block is listening to the message from the results block and resets itself.
It was fun to get this working. And surprisingly, the code for Arbiter.js is pretty small. Perhaps you'll be able to use it in an upcoming project.