Buses and Observers
The other code of interest is the Event Bus and Observer classes previously mentioned. The Event Bus class lets you marshal observers for a given event. These are not browser-based events but code-based ones. You can create as many as you want and your observers will contain the code necessary to process these events when they happen. In the case of this sample application, there are only two events of interest. When we initiate a newAjax request, we want to increment the number of currently outstanding requests. The corollary to that action is that when an Ajax action completes, we want to decrement the number of outstanding events. To register the observer, we include this line in the pageLoad() method:
Now let's take a look at the EventBus.js code (available online). Whenever we want to fire an event, we call EventBus.notify() with a message (or event name) and a payload. The message is used to determine which observers to notify.
When registering an observer, you specify two parametersan instance of the observer itself and the message to subscribe to. Outside of its constructor, RequestWatcher.js (available online) implements the single method, notify(). Notify takes the payload, in this case the number of remaining requests, and updates the output container with it. When we instantiated it in pageLoad(), the parameter we handed it was requestCount. This corresponds with the ID of a span tag in the HTML to be updated.
By selecting the right tools for the job, even a three-way mash-up like this can be thrown together quickly. In the case of FNN, we chose the Zend Framework as the base because it contains all the pieces we needed to get our service written quickly. Additionally, because of the way the Zend Framework is structured, we were able to easily pull out the pieces we wanted and use them independently.
Second, by using good object-oriented techniques and practices, the server-side piece is now a framework that can be extended to consume resources from other services by building new Proxy classes.
On the client side, the OO is a bit more difficult because of language limitations, but we adhere to OO design theory whenever possible. That lends itself to well-segregated code.
Finally, the one unspoken concept we coveredbuilding web services, even services that consume other's services, is painless. FNN talks to a simple PHP page named "service.php," which consumes other's services, but also publishes its own service. It converts the data from our news feeds, Yahoo, and Flickr, into an easily digestible format.
With the tools we've examined, you can now easily find the unique data or service that you have and build a service around it.