Online Book Reader

Home Category

AJAX In Action [224]

By Root 4091 0
The first function argument is the command name. All remaining arguments are treated as request parameters. The URL that we have constructed is then passed to the ContentLoader f, and the request is sent with the request parameters in tow g, as illustrated in the example usage shown previously. With this method in place, each of our portal command APIs will have a nicely minimal implementation. Another “for free” feature of having a generic method like this is that we can support new commands that become available on the server without having to change any client code. For now, let’s look at the commands we do know about.

Licensed to jonathan zheng

Refactoring

461

Login

Recall that our login button’s onclick handler initiates a call to the login() method of our page, which in turn calls this method. The login command, at least from the perspective of the server, is a command that the server must handle by checking the credentials and then (if they are valid) responding with the same response that our load-page command would perform. With that in mind, let’s look at the implementation shown in listing 11.15.

Listing 11.15 The portal login method

login: function(userName, password) {

this.userName = userName;

this.password = password;

if ( this.options.messageSpanId )

document.getElementById(

this.options.messageSpanId).innerHTML =

"Verifying Credentials";

this.issuePortalCommand( Portal.LOGIN_ACTION,

"user=" + this.userName,

"pass=" + this.password );

},

The method puts a “Verifying Credentials” message into the span designated by our configurable option this.options.messageSpanId. It then issues a login command to the portal back end, passing the credentials that were passed into the method as request parameters. The issuePortalCommand() method we’ve just put in place does all the hard work.

Load settings

Recall that the createPortal() function of our page calls this method to load the initial configuration of our portal windows. The method to load the settings for the page is even simpler than the login method just discussed. It’s just a thin wrapper around our issuePortalCommand(). It passes the user as the lone parameter that the server uses to load the relevant window settings, since the settings are on a per-user basis:

loadPage: function(action) {

this.issuePortalCommand( Portal.LOAD_SETTINGS_ACTION,

"user=" + this.userName,

"pass=" + this.password );

},

Licensed to jonathan zheng

462

CHAPTER 11

The enhanced Ajax web portal

Save settings

The save settings method is equally simplistic. Recall that this method is called by our AjaxWindows.js library adaptation on the mouseup event in order to store all move and size operations:

saveWindowProperties: function(id) {

this.issuePortalCommand( Portal.SAVE_SETTINGS_ACTION,

"ref=" + id,

"x=" + parseInt(elemWin.style.left),

"y=" + parseInt(elemWin.style.top),

"w=" + parseInt(elemWin.style.width),

"h=" + parseInt(elemWin.style.height) ); elemWin = null;

},

Adding/deleting windows

Although we didn’t fully develop the concept out of adding and deleting windows, at least from the perspective of providing a nice UI to initiate these actions, we can certainly define the command API methods that would support these operations, as shown here:

addWindow: function(title, url, x, y, w, h) {

this.issuePortalCommand( Portal.ADD_WINDOW_ACTION,

"title=" + title,

"url=" + url,

"x=" + x,

"y=" + y,

"w=" + w,

"h=" + h );

},

deleteWindow: function(id) {

var doDelete =

confirm("Are you sure you want to delete this window?"); if(doDelete)

this.issuePortalCommand( Portal.DELETE_WINDOW_ACTION,

"ref=" + id );

},

This concludes our discussion of the APIs required to support the portal commands. Now let’s look at our portal Ajax handling. 11.6.4 Performing the Ajax processing

As already noted, in this example we’re using an Ajax technique for handling responses in a script-centric way. The technique relies on the fact that the Licensed to jonathan zheng

Refactoring

463

expected response

Return Main Page Previous Page Next Page

®Online Book Reader