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 />

Views is capable of creating very <strong>com</strong>plex queries against the <strong>Drupal</strong> database. It<br />

does the heavy lifting automatically, without requiring you to (for example) write<br />

the SQL to retrieve information from the database. Part of this automation process<br />

requires the generation of field names that are unlikely to conflict with other fields in<br />

the returned results. Therefore, it uses a rather <strong>com</strong>plex naming convention to assign<br />

field names.<br />

We now have our view constructed. We will now turn to the <strong>JavaScript</strong> code that<br />

will run on the client.<br />

The <strong>com</strong>ment watcher<br />

We have just configured the server to give us the information we need in order<br />

to find out what the latest <strong>com</strong>ment is, using AJAX <strong>and</strong> JSON. In this section, we<br />

will develop a <strong>JavaScript</strong> tool that will use this information in order to generate<br />

<strong>com</strong>ment notifications.<br />

The script will watch the JSON feed for changes. When a new <strong>com</strong>ment appears in<br />

the JSON feed, the <strong>com</strong>ment watcher will notify the user.<br />

To create our script, we are going to draw on our existing toolkit, using behaviors,<br />

theming, <strong>and</strong> <strong>jQuery</strong> to build the <strong>com</strong>ment watcher.<br />

Let's first take a glance at the code in its entirety. We will then take a closer look at<br />

each section. This code is from the script <strong>com</strong>mentwatcher.js, which is loaded into<br />

our theme using a scripts[] = <strong>com</strong>mentwatcher.js directive in the info file:<br />

// $Id$<br />

/**<br />

* Watch for new <strong>com</strong>ments <strong>and</strong> display a message when a <strong>com</strong>ment<br />

* is posted.<br />

*<br />

* @file<br />

*/<br />

var CommentWatcher = CommentWatcher || {settings: {}};<br />

CommentWatcher.settings.path = 'newest_<strong>com</strong>ment';<br />

CommentWatcher.settings.maxLength = 128;<br />

CommentWatcher.settings.showSeconds = 7;<br />

CommentWatcher.settings.checkSeconds = 10;<br />

<strong>Drupal</strong>.behaviors.<strong>com</strong>mentWatcher = function () {<br />

if ($('#<strong>com</strong>ment_watcher').length == 0) {<br />

$('body').append('');<br />

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