Balance Rock Media

ActionScript and Ternary Statements

Written on 12/10/09 by Balance Rock Media

The ternary statement is a great way to reduce the lines of code you have to write for common tasks such as setting a checkbox based on a stored variable. This assumes that someVariable is a boolean value.

JavaScript Self Invoking Functions

Written on 11/22/09 by Balance Rock Media

If you've ever looked at the jQuery source code or written a plugin, you've probably seen a function that looks like this:

This format is for writing functions is becoming more common recently. If we break it apart, it works like the following:

So why would you want to do this? Well it's a nice way to control scope in JavaScript where if you aren't careful, everything ends up in the global object. This technique uses functions to limit your code's scope.

The above makes name and sayName() private and not accessible outside of the main function. If you do want something to be publicly accessible you could do something like the following which makes use of a namespace and closures to allow sayName() to run outside and still access the name variable:

Searching XML with E4X

Written on 11/1/09 by Balance Rock Media

Working with XML is one of the most common tasks when developing in Flash and Flex, yet it's not always clear how to search for a specific piece of content. The following shows how to get to an element by both it's index, and a piece of text.

The trick to the above code is understanding the double dot operator and the scope of the expressions contained in parenthesis. The following snippet's first line uses .. to get all cd nodes within xmlData regardless of where they are in the XML tree. In the second line the scope of the expression is limited to the song nodes.

Creating a JavaScript Confirm Popup

Written on 10/30/09 by Balance Rock Media

Have you ever clicked a link on a webpage only to realize that you just deleted something on accident? As a developer you can help your users prevent this by using the JavaScript confirm() function. This function produces a popup box similar to an alert, but gives the user a choice of "ok" or "cancel". The confirm function has a return value of true or false, so if you return this function when a user clicks cancel, it's the same as writing return false and canceling the default action.

Balance Rock Media is lead by Daniel Johnson and utilizes the design talents of James Kwon and Shaun Moynihan. The name Balance Rock comes from two large boulders on Wachusett Mountain that were stacked one on top of the other by moving glaciers thousands of years ago.