Online Book Reader

Home Category

Facebook Cookbook - Jay Goldman [109]

By Root 752 0

myDialog.setContext(document.getElementById('attachToMe'));

myDialog.showMessage('Message for you!', messageText, button_confirm='Thanks!');

//-->

That should give you a dialog something like Figure 7-7.

Figure 7-7. Contextual dialog with an embedded

Avoiding Heartache When the DOM Changes


Problem


I’ve built an amazing piece of JavaScript that has suddenly stopped working because Facebook has changed the structure of the page!

Solution


Be careful when you architect your code so that you don’t build very specific DOM structures into it:

this.getElementByTagName('div')[1].getFirstChild()

.getLastChild().setStyle('color', 'white');

It’s a much better idea to assign everything on your page a unique id or to give all similar objects a shared class so that you can target them more specifically from your JavaScript.

Discussion


The statement just shown is fragile for at least two reasons:

It’s effectively chaining down to the last child of the first child of the second

on the page (remember that arrays in JavaScript are zero-indexed, so the first
would be [0]). You might inadvertently change your own markup and insert an element above what used to be the first child, or below what used to be the last one.

If you’re used to doing development in a more traditional environment, you might not run into too many circumstances in which the HTML in your page changes without you doing it. Consider what happens to your code when Facebook changes the way something like is actually output by the FBML parser. If Facebook adds an extra

, your count is now off.

All of this is not to suggest that you shouldn’t chain your JavaScript commands together. There are plenty of times when combining a few lines of code can help optimize your function by cutting down on computationally expensive operations, such as allocating memory. It’s perfectly acceptable, for example, to optimize a few lines like:

var messageDiv = document.createElement('div');

messageDiv.setTextValue('Hi there!');

messageDiv.setStyle('color', 'red');

messageDiv.setStyle('border', '1px red solid');

messageDiv.addListener('click', closeMessage);

into a more compact single line like:

document.createElement('div').setTextValue('Hi there!')

.setStyle({color: 'red', border: '1px red solid'}).addListener('click', closeMessage);

The main difference lies in the dependence of the code on external factors. In this case, the entire line is self-contained and won’t break, even if the entire structure of your page changes. Keep in mind that lines like that really benefit from a comment to remind you of what they’re doing when you come back to them in six months’ time, or to prevent your teammates from wanting to hunt you down and express their frustrations in very real and physical terms. Remember Golding’s Law: always program as if the person who will be maintaining your program is a violent psychopath who knows where you live.

Linking to External FBJS Files


Problem


I have a large block of FBJS that I use throughout my application, and I’d really like to have the browser cache it for better client-side performance.

Solution


Facebook Platform supports linking to external FBJS files through the

Your external file will get included in your rendered FBML pages, with a cache policy set to never expire.

Discussion


As with most traditional JavaScript you put into your app pages, your

You can access that URL directly (not the one here, but the one from your own

®Online Book Reader