06.05.2013 Views

Drupal 6 JavaScript and jQuery - Ebook-Cours.com

Drupal 6 JavaScript and jQuery - Ebook-Cours.com

Drupal 6 JavaScript and jQuery - Ebook-Cours.com

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

AJAX <strong>and</strong> <strong>Drupal</strong> Web Services<br />

This is the first part of the behavior that we will examine. While every behavior<br />

function is passed a context (cxt in the code), we don't need to use the context for<br />

since we know exactly which part of the document we are concerned with.<br />

The first part of this function is a conditional:<br />

if ($('#main.webclips-processed').size() > 0) {<br />

return false;<br />

}<br />

We have seen conditionals such as this in other behaviors we have written, but it has<br />

a twist. Remember that a behavior can be executed more than once. All registered<br />

behaviors will be executed when the page first loads. Then, anytime <strong>Drupal</strong>.<br />

attachBehaviors() is called, they will be executed again. Behaviors need to be<br />

written in such a way that they can safely be called multiple times without, for<br />

example, attaching duplicate content.<br />

In the past, we have avoided this problem by using a construct like this:<br />

if ($('#main:not(webclips-processed')) {<br />

// Do main behavior here...<br />

}<br />

The code inside the conditional is only executed if the behavior has not already been<br />

processed. This is fine for short behaviors.<br />

But our behavior is a long one, <strong>and</strong> using this pattern would result in our wrapping<br />

a large block of code inside a conditional. It is considered a good programming<br />

practice (especially in object-oriented languages) to avoid this kind of nesting. There<br />

are two good reasons for this:<br />

•<br />

•<br />

Catching an "undesirable" case (where there is nothing to do) is best done<br />

early in a function because it is easier to read. We read functions from top to<br />

bottom. It is convenient (<strong>and</strong> easier to debug) to have these cases caught at<br />

the beginning, rather than at the end of the function.<br />

Nested code is harder to read. The further the indentation goes, the harder<br />

it is for a programmer to determine how the function works. Every layer of<br />

nesting requires the programmer to remember more context. Reducing the<br />

burden on the programmer also reduces the likelihood of bugs creeping in.<br />

So, instead of using something like if ($('#main:not(webclips-processed)) {},<br />

we take the opposite approach. We check to see if the element has been processed,<br />

<strong>and</strong> if it has, we return early.<br />

[ 210 ]<br />

This material is copyright <strong>and</strong> is licensed for the sole use by Richard Ostheimer on 18th June 2009<br />

2205 hilda ave., , missoula, , 59801

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!