Falling in Love with Aptana 1 comments   |   Tags: JavaScript   Aptana   |   Sorta-Perma-Link
Yeah, so I'm really dumb.  For a long time, I've used DreamWeaver pretty much exclusively as an IDE for my web projects.  There's something that makes sense here, as it has pretty good HTML and CSS features, and some limited ColdFusion and XML niceties.  However, it is the suck when it comes to JavaScript editing.

Today I discovered Aptana.  Of course, I've used Aptana before...by used, I mean, of course, that I downloaded the free version and played around with it (especially the built in AIR support).  However, I had never really used it to modify JavaScript.

Well, there is a new love in my life!  Aptana is smart enough to take my JavaScript, spaghetti-code as it is, and self-document all of the functions and variables that I have set.  They may not seem like a big deal, but when you have several hundreds of lines of JS code with inline comments, it can be a big pain to scroll around trying to find this or that.  

With Aptana, this kind of stone-age button pecking is over.  Aptana has a nice, built-in "Outline" toolbar that helpfully shows all of the functions in the selected file, along with any declared variables.  Plus, each function and variable is linked so that you can quickly and easily jump to the appropriate function/variable in an instant.  

Yes, so I am a noob to be just now figuring this out.  But you have to admit, it's pretty flippin' cool :)
Using Spry Data with Thickbox 0 comments   |   Tags: Spry   jQuery   Thickbox   |   Sorta-Perma-Link
If you've ever tried to use Spry data sets with a default implementation of Thickbox (a jQuery-based version of the familiar Lightbox), you've probably noticed that it doesn't work.

The reason for this, of course, is simple: out of the gates, thickbox.js fires off an initialization function (tb_init) through jQuery's ready().  What this means, technically, is that thickbox has already started its processing and element fishing before Spry's data sets have been fully loaded and rendered.  What this means, practically, is that Thickbox won't work.

Though frustrating, there is a fairly simple way to work around this.  

Conceptually, what we'll be doing is to bypass jQuery's ready() function and load tb_init manually AFTER we know Spry's data sets have fully loaded and rendered.

First, we need to figure out when the data sets are done processing.  This is pretty simple, because Spry comes with a handy way of sniffing this out.

We'll start by setting up a new data region observer.  Our observer will watch the data set and when it reaches the "state" that we define, we can manually fire the thickbox.js processing.

So here's our new observer:

the_Observer= new Object()
the_Observer.onPostUpdate = function() {
   // Here's where the thickbox function call will go...    
};


Spry.Data.Region.addObserver("the_region", the_Observer);

Nothing too complicated here.  We create a new Spry region observer, tell it to watch for the "onPostUpdate" state, and then we can load in whatever processing we want to the function we've defined for that state.

The next step, then, is to load in the thickbox initialization function.

To do this, start by going to the thickbox.js file.  Copy the following lines:

tb_init('a.thickbox, area.thickbox, input.thickbox');
imgLoader = new Image();// preload image
imgLoader.src = tb_pathToImage;


Once you have them copied, go ahead and comment out the entire $(document).ready function, including the lines you just copied.  Save and close.

Go back to your other page and paste the three lines into our observer function.  The final result will look something like this:

the_Observer= new Object()
the_Observer.onPostUpdate = function() {
    // See?  Here's the init function for thickbox...
    tb_init('a.thickbox, area.thickbox, input.thickbox');
     imgLoader = new Image();// preload image
     imgLoader.src = tb_pathToImage;
};

Spry.Data.Region.addObserver("products", the_Observer);


That's all there is to it.  Now your Thickbox content should work correctly.
Checking if Spry eventListener Exists 0 comments   |   Tags: Spry   eventListeners   |   Sorta-Perma-Link

So if you use Spry, you know that adding an eventListener to anything is super-simple.  It's easy to add, and managing the processing that occurs on events is pretty intuitive. 

For example:

Spry.Utils.addEventListener("myButton","click",doMyFunction,false);

This one line of code adds an eventListener to the element "myButton" for the onclick event, which will fire "doMyFunction()".  And it's just as easy to remove the same eventListener:

Spry.Utils.removeEventListener("myButton","click",doMyFunction,false);


Easy enough.  However, I recently found myself wanting to not only add and remove eventListeners, but also to check if they exist for certain elements.  Luckily, before digging into the DOM to much, I checked SpryDOMUtils.js--sure enough, there's a function already built for this.

In fact, as you may or may not be aware, you can run Spry.Utils.addEventListener() on an element as many times as you like--only one eventListener will be registered.  This is because addEventListener() runs a check to see if the eventListener is already registered, and proceeds from there (look at line 166 in SpryDOMUtils.js v. 0.6 to see this in action).

So checking to see if an eventListener is registered for an element is exactly the same as adding and removing the eventListener.  Here's what it looks like:

Spry.Utils.eventListenerIsBoundToElement("myButton,"click",doMyFunction,false);

This will return true or false, and you can do whatever you'd like from there.