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.

[ 265 ]<br />

Chapter 8<br />

Right now, we have the buttons defined in $buttons. Should these be added to the<br />

<strong>Drupal</strong>.settings object or sent as a script fragment? Technically speaking, either<br />

one can be done.<br />

Adding things to the <strong>Drupal</strong>.settings array has the advantage of being easier to<br />

code (by a few characters). Modules, such as <strong>JavaScript</strong> Theming, make liberal use of<br />

<strong>Drupal</strong>.settings. But this is not actually a good idea most of the time.<br />

Earlier in the book, we talked about namespaces. The chief rationale for namespacing<br />

applies even here. If we just add our settings to <strong>Drupal</strong>.settings, we may<br />

eventually encounter problems where other <strong>JavaScript</strong> files which use settings with<br />

the same name. This will lead to conflicts as two different libraries will try to store<br />

their data in the same place. The <strong>Drupal</strong> API docs (http://api.drupal.org/api/<br />

function/drupal_add_js/6) have this to say on the matter:<br />

You might want to wrap your actual configuration settings in another variable to<br />

prevent the pollution of the <strong>Drupal</strong>.settings namespace.<br />

Is the suggestion that we ought to wrap settings in another variable <strong>and</strong> then use<br />

<strong>Drupal</strong>.settings (creating something like <strong>Drupal</strong>.settings.BetterEditor.<br />

buttons), or that we should use an altogether different variable?<br />

The first option is not good. Frankly, repartitioning a namespace like this is silly.<br />

We've now created another namespace inside of a namespace, <strong>and</strong> for what purpose?<br />

In addition to this, calling the function is even more difficult. That's because we have<br />

to add another layer of testing to our array. Our array would look more like this:<br />

$buttons = array(<br />

'BetterEditor' => array(<br />

'buttons' => array(<br />

array(<br />

'name' => 'B',<br />

'tag' => 'strong',<br />

// ...<br />

),<br />

array(<br />

'name' => 'I',<br />

// ...<br />

),<br />

// <strong>and</strong> more buttons....<br />

),<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!