X: Pt4. Object Literals, Convenience Methods, And Go!
- October 24th, 2009
- Posted in Code . JavaScript
- Write comment
Now that we have a shiny new object created called ‘X’, Let’s use it. I like to organize all of my JavaScripts by incorporating object literals (see Rebecca Murphey’s talk on the closely related object lateral [that's a joke btw, it was a misprint at jconf], the excellent book Simply Javascript, and of course The Good Parts ). Here’s a ‘stub’ for the objects we could use:
cache: {},
config: {},
init: function() {},
otherThing: function() {}
}
I will typically use the ObjectName.cache{} to hold a snapshot of the page, or areas of the page that I don’t want to have to re-query the DOM for. If my page is not going to change via AJAX I can store fetched results there for further use/manipulation. The ObjectName.config{} has the same functionality as .cache{}, it’s just my way of seperating out more important info than the generic .cache{}. You could use one, both, or neither, really just a matter of taste.
For this to be of use we need a way to load it once the dom is ready. Let’s add a ‘ready’ type event to the X library then:
X.addEventHandler(window,"load",obj.init);
};
You’ll pass your object literal to the .go() method which will, after the page has loaded, call the init() method of the object you passed to it:
Where this goes depends on how you are constructing your site. For now, we’ll put this line just below each object literal created. Later we’ll look at a site-wide answer to automating the loading of scripts, see Paul Irish’s blogs for more on that…
With a way to bind event handlers, object literals to hold the handlers, and a method to load the scripts, now we can just write some really basic JavaScript to utilize this stuff. So maybe there’s a button that lives on a page:
We need to set an event handler on that button to fire when the user clicks it. In the init() function of objectName do something like:
var button = X.get("#say-hi");
X.addEventHandler(button, "click", ObjectName.doClick);
}
I’ll get to the ‘doClick’ function in a moment. First, I want to write myself a convenience method to shorten the verbosity of adding event handlers. This is optional, of course.
X.addEventHandler(element, 'click', handler);
Now, with the convenience method added to the X library we can revisit what we wrote earlier:
X.$click(button, objectName.doClick);
That’s getting better, but we can combine those 2 statements down to:
I am using a ‘$’ as a sigil in front of the name of the convenience methods throughout ‘X’. I do this to keep any conflicts with keywords like ‘submit’, ‘blur’ or ‘focus’ from happening. Plus all the cool kids are using $…
OK, so about the doClick handler. Remember that X.addEventHandler takes a target element, an event, and a function to call when that event happens on that element. The doClick function call is in object literal notation here, it is an attribute of the object you have made. I’ve been using ObjectName as the object so far (the observant among you may have noticed that the file is objectName.js. I do this in the development phase of a project as I will usually have many separate objects, all of them their own individual .js files. So objectName.js will have one object defined in it, ObjectName. At deploy time, I’ll combine them into one .js file and minify it). ObjectName will have this named function as an attribute:
alert("Hi!");
}
The event handler calls ObjectName.doClick() because you bound it to that element. Here’s the whole object then:
init: function() {
X.$click(X.get("#say-hi"), objectName.doClick);
},
doClick: function(event) {
alert("Hi!");
}
};
X.go(ObjectName);
Don’t forget to include the call to the go() method, outside of the variable which passes in ObjectName and tells the browser to call ObjectName’s init() method when the DOM fires the ‘load’ event. The cool thing about an object like this is re-usability. If your site was littered with buttons which needed to say “Hi” when pressed (it could happen) you would only need to attach this object to a master page and it would attach the event to any button with an id of “say-hi”. Now, it is an id so there should only be one per page. If there are going to be multiple instances per page, use a class instead, and iterate over the nodelist that get returns with a for loop to attach events. Assuming there are multiple buttons on the page with a class=”say-hi” attribute you would do:
config: {
say: "Hi!"
},
init: function() {
var buttons = X.get(".say-hi");
for (var i = 0; i > buttons.length; i++) {
X.$click(buttons[i], ObjectName.doClick);
}
},
doClick: function(event) {
alert(ObjectName.config.say);
}
};
with this you wouldn’t have to worry about the number of elements with a class of “say-hi”, as all of them would be fitted with a click event handler that will call ObjectName.doClick(). Note the use of the config object which is an attribute of ObjectName. You can manipulate this attribute if you wanted, changing it during the page lifecycle, like say adding a toggle-type command in the doClick() function:
alert(ObjectName.config.say);
ObjectName.config.say === "Hi!" ? ObjectName.config.say = "Hi Again!" : ObjectName.config.say = "Hi!";
}
Now the alert will alternate between “Hi!” and “Hi Again!”. Silly, but you get the idea.
Next, we’ll do a continuation of the object literal using more of the config and cache functionality, I need to get away from this keyboard now!


No comments yet.