|
YOUR FEEDBACK
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
TOP THREE LINKS YOU MUST CLICK ON Real-World AJAX Book Preview AJAX Load Buddies
When the AJAX request is complete, it calls the display() function
By: James Benson; Jay Fienberg
May. 15, 2007 06:45 PM
This content is reprinted from Real-World AJAX: Secrets of the Masters published by SYS-CON Books. To order the entire book now along with companion DVDs for the special pre-order price, click here for more information. Aimed at everyone from enterprise developers to self-taught scripters, Real-World AJAX: Secrets of the Masters is the perfect book for anyone who wants to start developing AJAX applications.
Load Buddies This load() function gets called regularly by our refresh() loop. And, it's not until refresh() is called for the first time that the buddy list is loaded. This is why refresh() is called at the end of the login() function, see Listing12.13b. Listing 12.18 Load
load: function () { As you can see in the example, there are no parameters to pass in the request; all of the values the API requires get encoded in the request URL. This function makes requests that look like /im.php/html/John/buddies.
Display Buddies We also attach a new Event listener to each buddy name that appears in the buddy list. These listen for the "click" event, and trigger our goChat() function (see Listing 12.23). You will notice that we also use two nice features of the Prototype library: document.getElements-ByClassName() method and the Ruby-style iterator that runs through the buddy list. Listing 12.19 Display
budlist: '',
Change Status If the user sets the status to "Offline", this function also calls the global logout() function (which we'll cover in more detail in Listing 12.34), which more fully "takes the user offline" from the point of view of both the IM Client and the IM Server. Listing 12.20 Change Status
changeStatus: function () { Notice how the "this" keyword refers to the instance of our Buddies object, and not the HTML element the Event listener is bound to. Since we do need to access that HTML element, we do so through the object's this.statusControl property, in which we stored that HTML element exactly for this kind of access to it. Also note: we have a status_msg variable set to the empty string. We had planned to add the means for the user to set a comment when they updated their status. This feature is built-out in the IM Server code, and there are stubs for it in the Buddies object. But, it was not built into the interface, and so there is currently no way for the user to set a comment.
Post Status Update In this case, we are using the POST method on the AJAX requests, and so we use the postBody option to set the request parameters. Again note that in our current implementation, status_msg passed to this function will always be the empty string. But, you can see in this code that this value is encoded and sent to the server. Listing 12.21 Post Status Update
postStatusUpdate: function (status, status_msg) {
Destroy Buddies Besides setting the buddy list display style to "none" in order to hide the buddy window, this function has the important job of stopping all of the Event listeners we've added. To do this, we use the Prototype library's Event.stopObserving() function, which has the reverse effect of Event.observe(). This is a context where, again, there can be issues around the binding of the "this" keyword. Our this.changeStatusObserver property stores the properly referenced object that needs to be passed into Event.stopObserving(), and we recommend you compare how we use Event.observe() in Listing 12.17 with how we use Event.stopObserving() in Listing 12.22. Listing 12.22 Destroy
destroy: function() {
Go Chat Because goChat() does some management of our chat windows, we decided not to encapsulate it within the Buddies object. Conceivably, one could encapsulate this in Buddies, and each chat window would then be an object dependent on the buddy list. This dependency is somewhat the case, but we felt there might be other cases when chat windows are managed outside of the buddy list. So, we made goChat() an independent function. Becuase goChat() is bound to the HTML elements that represent the buddy names, the "this" keyword refers to the element where the event was triggered. Listing 12.23 Go Chat
function goChat() { In Listing 12.10c, we showed the HTML returned from the server, and how each buddy name is embedded in the DIV id, like: <div class="budlisting" id="buddy_Ringo">. In goChat() we use these IDs to uniquely identify the chat windows. We have an array windowMgr that is defined globally in im.js, and we check to see if windowMgr[this.id] exists, where this.ID is an ID like buddy_Ringo. If it does not already exist, we append a new item on the windowMgr array identified by the ID, and we set this new item to be a ChatWindow() object (which we'll cover below, Listing 12.24-32). We also have a windowsUsed counter variable that is global in im.js. We use this to give each chat window a number. These numbers, each of which we call a "handle," connect the ChatWindow object with the "index" of the HTML elements that we described at the beginning of the chapter, in Listing 12. 5. Finally, we call the load() and openOrFocus() methods on the chat window.
ChatWindow Objects Also like the Buddies class, the ChatWindow class' initialize() method sets a number of properties that we'll use in other methods of the ChatWindow, and also sets a number of Event listeners. The Event listeners in the ChatWindow listen for the following:
ChatWindow.prototype = { One trick we do in intialize() is parse out the name of the buddy from the ID that is passed to the ChatWindow at the time of object creation. Our IDs look like buddy_Ringo, and we just parse out what comes after the buddy_ prefix and take that to be the buddy name. This name is used in requests to the server as the "to" parameter.
Open or Focus Chat Windows Listing 12.25 Open or Focus
openOrFocus: function () {
Load Chat Messages This load() function gets called regularly by our refresh() loop. Listing 12.26 Load
load: function() { Note that the requests to the server will be in the form /im.php/html/John/msgs/Clarence.
Display Messages We also scroll the chat window to the bottom. Listing 12.27 Display
display: function(xhr) {
Chat Send Message Our chatSent() function captures these events and then calls the object's sendMessage() method. Notice how the Event is passed as a parameter to this function. This is a feature of Prototype's bindAsEventListener() method that we're using. It makes the "this" keyword refer to an object other than the HTML Element to which the Event is bound, but it also passes that Event as a parameter so it still can be accessed. Listing 12.28 Chat Send
chatSend: function(ev) { Another small feature of this function is that it resets the value of the text input and resets the focus on the input after the message is sent. This is an expected behavior that must be explicitly created - especially for the case where the user is clicking the send button, which otherwise has no connection to this input.
Send Message In this case, we are again using the POST method on the AJAX requests, and so we use the postBody option to set the request parameters. Listing 12.29 Send Message
sendMessage: function() {
This content is reprinted from Real-World AJAX: Secrets of the Masters published by SYS-CON Books. To order the entire book now along with companion DVDs, click here to order. LATEST AJAXWORLD RIA STORIES
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||