Ajax and Ajax Frameworks

I have been a developer for nearly 20 years and a web developer for about 10 years. Over the years, I have considered creating a blog to share the things I have learned (and am learning) about web design and development, but I never seemed to have the time. When I began learning about web development using Ajax and Ajax Frameworks, I decided to take the time to create this blog.

My intention for this blog is to focus primarily on Ajax Frameworks, but since web development requires knowledge of many technologies, I will occasionally write about things such as CSS, Javascript, PHP, MySQL, Flash, etc.

Lately, I have been using JQuery as my primary Ajax tool. Although I have used other Ajax Frameworks in the past (Dojo Toolkit, Yahoo! User Interface Library, Scriptaculous/Prototype), JQuery has thus far been the easiest to learn. In my first few posts I will discuss some of the JQuery Plugins that I have created. Some of these include: a plugin for Google Maps (jquery.imGoogleMaps), Form validation and submission  (jquery.imValidateForm), Page Populater (jquery.imPagePopulate), and a plugin to create lists (jquery.imList).

While I am still learning about some of the other Ajax Frameworks, I hope that what I have learned will be helpful to others. Just remember, “When you can pull the pebbles from my hand…”

Share

YUI Dialog Buttons Scope

Saturday, July 18th, 2009

When calling a function to handle the click event for the YUI Dialog buttons, the scope of the function is always the dialog instance itself. This is not a problem in most situations, but when creating a custom YUI widgets, use the object literal option to handle the event rather than just calling a function. Otherwise, you will run into scope issues.

I usually create a  YUI Dialog in a fairly standard way:

?View Code JAVASCRIPT
var dlgContainer = new Dialog("dlgContainer", {
     width: "75px",
     buttons: [ { text:"Submit", handler: handleSubmit, isDefault:true },
	        { text:"Cancel", handler: handleCancel } ],
     draggable:false,
     close:true,
     fixedcenter : true,
     constraintoviewport : true,
     modal: true
});

And then the handlers:

var handleSubmit = function() {

};

var handleCancel = function() {

};

This works fine. Placing this same code in the init method of a YUI Widget:

?View Code JAVASCRIPT
...
init:function(oConfig) {
   this.container = new Dialog(this.container, {
        width:"175px",
	buttons: [{ text:"Submit", handler: this.handleSubmit, isDefault:true },
	          { text:"Cancel", handler: this.handleCancel } ],
	draggable:false,
	close:true
        ...
   });
},
handleSubmit:function() {
     if (this.validate()) {
 
     }
},
handleCancel:function() {
     this.cancel();
     ...
},
validate:function() {
     ...
}
...

The handleSubmit method calls this.validate (a method of the widget). Because the scope of handleSubmit is the dialog instance, the validate method of the widget will not fire (out of scope). In order to change the scope of the dialog buttons click handler, you have to use an object literal:

?View Code JAVASCRIPT
...
buttons : [ { text:"Submit", handler: {fn:this.handleSubmit, obj: this, scope: this}, isDefault:true },
	      { text:"Cancel", handler: this.handleCancel } ]
...

The object literal for the YUI Dialog buttons handler takes the format:

{
fn: Function, // The handler to call when the event fires.
obj: Object, // An object to pass back to the handler.
scope: Object // The object to use for the scope of the handler.
}

In my example,

{fn: this.handleSubmit, // a method of the widget

obj: this,  //I’m passing the widget as the object

scope: this //I want the widget to be the scope of the handler

}

I hope this was helpful.

Share

CommentLuv badge