October 14, 2007

My Singleton Instance Dom Hijacker

var DomHijacker = function(el) {

var init = function() {
};

return function() {

Event.onAvailable(el, init);

return {

/* public functions here */

};

}();

}('htmlElementId');

What is so special about this? After all, it looks just like what we are all used to. It uses closure, it uses self invoking functions, and like everything else, it uses the YUI. The only new and cool thing about it is that it is completely self contained. It calls its own init function. Note: A full example of this function is posted here.

The point of the whole thing is that the function will only begin to modify DOM elements after they are ready. Allow me to explain each line:

var DomHijacker = function(el) {
This line simply declares a variable DomHijacker, which will be a function. It takes one parameter, a string called el (element) which is the id of a Dom element on the page.

var init = function() {
};
Within DomHijacker, there is an init function. Init stands for initialize, because this function will run procedures on the Dom element with the id 'el'

return function(){
The function, or 'class' in this case called DomHijacker will return a function. This is called closure. Any objects that are not in this returned function will not be given back so to speak. For instance, you could not try DomHijacker.init() somewhere else. Now, DomHijacker is actually this anonymous function. This function will not be called until someone says DomHijacker(); it will simply be stored in DomHijacker, but not invoked.

Event.onAvailable(el, init);
We will use YUI's event utility to wait for our html element with the id 'el'. You can see how onAvailable works here. When our html element is ready, the init function will be called. Although I do not use it, it is important to note that the keyword 'this' will refer to the Dom element with id 'el' within init after YAHOO.util.Event.onAvailable calls it.

return {
/* public functions here */
};
Let's just perform another closure. Why not. This is the final closure, and will contain any public functions. For example, within the big brackets, we could say:

hide: function (){
Dom.setStyle(el, 'display', 'none');
}
This is a public function which uses YAHOO.util.Dom.setStyle to make our Dom element with id 'el' go away. To used it we would say DomHijacker.hide(); The naming convention of my DomHijacker is a little weird, but I will give a better example below. If I was going to 'hijack' a list and turn it into some sort of dynamic menu, I would have called DomHijacker, CustomMenu. Then saying CustomMenu.hide(); would sound a lot more acceptable.

}();
Of course, if we want init to Event.onAvailable(el, init); to get called, we must invoke the anonymous function which we were returning in the first place. Now that code will get run by JavaScript, and YAHOO.util.Event will start waiting on 'el'. This is good. The whole reason that we have to wait on 'el' is because the JavaScript gets started before the rest of the Dom elements are actually... well... Dom elements (in the Dom). If you are still with me, you are freakin' awesome! If not, well, congrats on even reading this far. Feel free to ask questions.

}('htmlElementId');
This is the final line in the function. Remember how on the first line, DomHijacker wanted a string for the 'el' which is the id of the html element? Well, we are self invoking DomHijacker and at the same time passing it that very string. 'htmlElementId' gets squished into the variable we called 'el' and is available all throughout DomHijacker, our 'class'. Remember? This is the id that onAvailable was waiting for.

The whole idea here is that our big class called DomHijacker wants to modify an html element. We cannot do that until that element is in the Dom; which is why we must wait on it; which is why YUI comes in so handy.

Lets add some functionality to our class.
We'll call it customList. First let's get a namespace to work in.

var DanW = {};
This will help keep things clean.

DanW.customList = function(el) {
I want to make that 'better example' I mentioned earlier.

var Event = YAHOO.util.Event;
var Anim = YAHOO.util.Anim;
var Dom = YAHOO.util.Dom;

var _item;
Establish some shortcuts for ease of typing. Also, make a 'private' variable, with the underscore prefixed naming convention called _item. This will be an array of the LI items in the list on the page. They will be references to the actual Dom nodes or elements.

var init = function() {
_item = Dom.getChildren(el);
Event.on(_item, 'click', action);
};
This is the init function, in all its glory. All we do is get the items in the list called el, and assign a function, action to their respective clickage.

var action = function() {
/* keyword this refers to
the li which was clicked on */
Dom.setStyle(this, 'color', '#FBB104');

var attributes = {};

attributes.fontSize = {
from: 100,
to: 200,
unit: '%'
};

var sizeUp = new Anim(
this,
attributes,
0.2,
YAHOO.util.Easing.easeOutStrong);

sizeUp.animate();
Event.purgeElement(this, false, 'click');
};
If you have been with me so far, I should really not need to explain all this. Although I will say that since I only want the items to be clicked once for the animation, I simply removed the click listener after one click. I did this by calling: Event.purgeElement(this, false, 'click');

Here is the entire function: customList, showing the self invokers at the bottom.

var DanW = {};

DanW.customList = function(el) {

var Event = YAHOO.util.Event;
var Anim = YAHOO.util.Anim;
var Dom = YAHOO.util.Dom;

var _item;

var init = function() {

_item = Dom.getChildren(el);

Event.on(_item, 'click', action);

};

var action = function() {
/* keyword this refers to
the li which was clicked on */
Dom.setStyle(this, 'color', '#FBB104');

var attributes = {};

attributes.fontSize = {
from: 100,
to: 200,
unit: '%'
};

var sizeUp = new Anim(
this,
attributes,
0.2,
YAHOO.util.Easing.easeOutStrong);

sizeUp.animate();
Event.purgeElement(this, false, 'click');
};

return function() {

Event.onAvailable(el, init);

}();

}('customList');
It has been a pleasure explaining this. Feel free to ask questions. A full example of this function is posted here.

No comments: