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.

[ 217 ]<br />

Chapter 7<br />

The WebClips.showItem() function<br />

The <strong>Drupal</strong>.behaviors.webclips() behavior we looked at earlier was <strong>com</strong>plex,<br />

performing several steps to set up the area where our web clips will be displayed. At<br />

the very end, it calls WebClips.showItem() to display an item. The present function<br />

is much simpler:<br />

WebClips.showItem = function () {<br />

var items = WebClips.items;<br />

var theOne = Math.floor(Math.r<strong>and</strong>om() * items.length);<br />

var item = items.eq(theOne);<br />

var theLink = <strong>Drupal</strong>.theme(<br />

"webclip",<br />

item.find('title'),<br />

item.find('link')<br />

);<br />

$('#webclips').html(theLink);<br />

};<br />

This function takes no parameters. The first thing it does is make a local copy of the<br />

WebClips.items <strong>jQuery</strong> object. This is only done for the sake of readability.<br />

Next, we get a r<strong>and</strong>om integer corresponding to a valid index in our items list. For<br />

example, if our list has three items, we want an integer between 0 <strong>and</strong> 2. We get this<br />

with Math.floor(Math.r<strong>and</strong>om() * items.length).<br />

To limit the matched items from a <strong>jQuery</strong> object to just one specific index number,<br />

we use the <strong>jQuery</strong> eq() method.<br />

Now that we have an item, we are ready to theme it, <strong>and</strong> insert it into the web clips<br />

area on the page. Here's the theming call:<br />

var theLink = <strong>Drupal</strong>.theme(<br />

"webclip",<br />

item.find('title'),<br />

item.find('link')<br />

);<br />

This uses the <strong>Drupal</strong>.theme.prototype.webclip() function, to which it passes: the<br />

title of the RSS feed item, <strong>and</strong> the link from the RSS feed item. The link, you may<br />

recall, points back to the original article that this feed item is describing:<br />

<strong>Drupal</strong>.theme.prototype.webclip = function (title, href) {<br />

return '&rarr; ' + title.text().link(href.text());<br />

};<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!