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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Drupal</strong> Behaviors<br />

<br />

<br />

<br />

<br />

This shows the <strong>com</strong>plete contents of one specific block. We are more interested in the<br />

generic structure that all blocks share. To see this, we can simplify the previous code<br />

to something like this:<br />

<br />

Title<br />

Content<br />

<br />

That's about all there is to a generic block. Every block has three structural pieces,<br />

<strong>and</strong> these are identified by class. There's a block that contains a title <strong>and</strong><br />

some content.<br />

Returning to our code, let's look at the behavior function:<br />

<strong>Drupal</strong>.behaviors.slideBlocks = function (context) {<br />

$('.block:not(.slideBlocks-processed)', context)<br />

};<br />

.addClass('slideBlocks-processed')<br />

.each(function () {<br />

$(this).children(".title").toggle(<br />

function () {<br />

$(this).siblings(".content").slideUp("slow");<br />

},<br />

function () {<br />

$(this).siblings(".content").slideDown("slow");<br />

});<br />

});<br />

Our behavior first tries to find all of the blocks that haven't already been processed<br />

by this behavior. The query to do this is: .block:not(.slideBlocks-processed).<br />

As we saw in the previous HTML, the class block indicates that a piece of HTML<br />

is a block.<br />

Again, we want to prevent our behavior from being run twice on the same<br />

element. To do this, we write the behavior in such a way that it attaches its own<br />

class to an element once the element has been processed. In this case, the class is<br />

slideBlocks-processed, following one of the conventions used when<br />

defining behaviors.<br />

[ 104 ]<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!