Listing 16.1. Instantiating ErrorManager (ErrorManager.js)
The onerror event will be used to notify the subject, which is the ErrorManager object of any errors that occur. ErrorManager will then notify any objects that have been registered and stored in its collection. There are three methods we will create, which we discussed in the previous section. The methods that will handle all the observation functionality are called registerObserver, notifyObservers, and unregisterObserver. These methods must be the first written in the object after the object has been declared. Listing 16.2 shows the registerObserver method.
Listing 16.2. Registering Observers (ErrorManager.js)
The registerObserver method does exactly that: It registers an observer by adding it to an observerCollection, which is a property of the object and is essentially an array that is specifically used to store observers. Observers that are added to this collection need to specify the object name plus the method they want to use as the notification callback in a codestring. A codestring is a string representation of any code. In this case, it would be a method call in the form of a string:
The collection will be declared in an initialize method that we will create as soon as we have finished adding the observer methods to the object so that we keep the scope of the object members intact.
The next method, called notifyObservers (see Listing 16.3), iterates through the observerCollection and fires the callback methods that the observers specified.
Listing 16.3. Notifying the Observers (ErrorManager.js)
The next method is the unregisterObserver method, which is used to remove observers from the collection so that they are no longer notified when an event occurs. Listing 16.4 shows the method as it is used in ErrorManager.
Listing 16.4. Unregistering Observers (ErrorManager.js)
Once we have these three methods created, we need to initialize the object and all its properties. Listing 16.5 shows our ErrorManager's initialize method.
Listing 16.5. Initializing the ErrorManager (ErrorManager.js)
The first notify method that we will create is the emailError method (see Listing 16.6) that we registered in the initialize method. This method contains the functionality to send an email to specified developers listing all the details of the error.
Listing 16.6. Emailing Errors (ErrorManager.js)
Take a look at the parameters the emailError method accepts. They are the three parameters that we previously discussed. We will use these parameters to create an HTML-formatted email that we will ultimately send to ourselves or the developer of our application. After we have the error formatted as HTML, we will identify the variables that will be used as the parameters in the email. The first parameter is the page that we are requesting through Ajax, which is an ErrorManager class that we will create with PHP shortly, and the second is the subject of the email. After we have our variables identified for the request, we will send it to the AjaxUpdaters's update method, which we created in Chapter 14, "Singleton Pattern." This request will be made through the POST method to the page that we specify, which in this case is an ErrorManager class that we will create with PHP. The request is sent along with a query string that consists of the variables we defined, such as the subject and HTML error. We will also specify a callback method, named catchResponse, which we can use to handle the response from the server. After all this information has been passed to the AjaxUpdater, it will make the request through the Ajax object to the PHP page specified.
Although we have not covered server-side interaction with Ajax, in this section we will be jumping ahead a bit in order to add the functionality to send the email. Later, in Part V, "Server-Side Interaction," we will take an in-depth look at server-side interaction. The PHP ErrorManager class is a fairly small class, so it will be a good object in which to make our first server-side interaction. Listing 16.7 shows the class in all its glory.
The PHP classes used throughout this book require PHP version 5.0.
Listing 16.7. ErrorManager PHP Class (ErrorManager.class.php)
Now that we have a fully functional ErrorManager object, we can use it throughout our application. Let's see how.