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 onClose Event

Wednesday, December 16th, 2009

I was surprised to find that the YUI Dialog does not have a onClose event. I have been working on a login dialog widget using the Yahoo User Interface Library. I want to display a message if the user closes the dialog.

Now I could use the ‘close:false’ option when I initialize the dialog to prevent the close button from appearing (the ‘X’ in the top right of the dialog):

?View Code JAVASCRIPT
this.container = new Dialog('login', {
        width:"175px",
	buttons : [ { text:"Login", handler: this.handleSubmit}],
	draggable:false,
	close:false
});

The problem is that the widget will have the option of creating a modal dialog and I want the user to be able to close it without having to refresh the page:

?View Code JAVASCRIPT
if (this.container_position == 'fixed') {
        this.container.cfg.queueProperty("fixedcenter", true);
        this.container.cfg.queueProperty("constraintoviewport", true);
        this.container.cfg.queueProperty("modal", true);
        this.container.cfg.fireQueue();
}

If the user closes the login dialog by clicking the ‘X’, I want a message to be displayed informing the user that they should login and I also want to display a button that will re-open the dialog. I searched but could not find an onClose event. But I eventually found a solution. Using Firebug, I saw that YUI creates the dialog’s close button with a class named ‘container-close’. So I added an event listener.

?View Code JAVASCRIPT
var Dom = YAHOO.util.Dom,
     $ = Dom.get,
     Event = YAHOO.util.Event;
 
var closeBtn = Dom.getElementsByClassName('container-close')[0];
var responseDiv = $('responseDiv');
Event.addListener(closeBtn, 'click', function(e) {
        responseDiv.innerHTML = 'You must login to continue <button id="btnShow">Login</button>';
}, this.container, true);

I added an event listener that displays a login message and button when the user clicks the close button of the dialog (the ‘X’). Notice that I added [0] at the end of the getElementsByClassName method. This is because getElementsByClassName returns an array of elements. I only want the first element in the array not the entire array.

Granted, this is not the solution that I prefer, but it works. I think that YUI should add an onClose event method for the dialog so that developers don’t have to create these types of solutions. But, this works, so I can move on. And I still think that YUI is a great Javascript framework.

Share

3 Responses to “YUI Dialog onClose Event”

  • RobertM Says:

    Did you try subscribing to the hideEvent?

    YAHOO.example.container.ContainerName.hideEvent.subscribe(function() {
    alert(“Dialog Closed”);
    });

    ContainerName would be the dialog name.
    You could also use beforeHideEvent.

  • admin Says:

    I created the code above because my attempts at subscribing to the hideEvent did not work. After reading your comment, I tried subscribing to the hideEvent again. This time it worked correctly. Interesting. Not sure why it worked this time. The only difference is the other day I was testing on my Ubuntu box and today I’m testing on my Mac. I’ll have to test it again on Ubuntu later today. Thanks.

  • RobertM Says:

    Here is a sample working based of the YUI 2.0 samples. http://www.robertmeffe.com/YUI2.0/examples/container/PanelCleanClosing.html

CommentLuv badge