The drupaljs library

In the Using jQuery in Drupal section of the previous chapter, we saw how Drupal will automatically include the jQuery library when it detects that the page request uses JavaScript. When a theme or module, for example, includes a JavaScript file with a scripts[] entry in the .info file, Drupal will automatically include the standard JavaScript libraries.

Let's take another look at the scripts that get included:

<script type="text/javascript"

src="/drupal/misc/jquery.js"></script> <script type="text/javascript"

src="/drupal/misc/drupal.js"></script>

<script type="text/javascript"

src="/drupal/sites/all/themes/frobnitz/printer_tool.js"> </script>

<script type="text/javascript">

jQuery.extend(Drupal.settings, { "basePath": "/drupal/" }); </script>

Drupal generated this code from our printer tool script developed in Chapter 2. There are three JavaScript files included here. The first one is jquery.js, which was the focus of the previous chapter. The last one is printer_tool.js, the script we created in Chapter 2.

Sandwiched between them is drupal.js. Like jquery.js, drupal.js plays a prominent role in Drupal's client-side scripting efforts, which is included by default on any page that includes JavaScript.

But what exactly does it do?

Essentially, it is composed of four classes of tools:

1. Theming functions

2. Translation functions

3. Utility functions

4. Support for Drupal Behaviors

Theming functions provide a partial JavaScript implementation of the Drupal theming system. Using the theming system, we can theme JavaScript objects just like the PHP developers do on the server side.

Translation functions, which we will explore in the next chapter, provide language translation facilities to JavaScript. Just as is the case with the theming system, the translation system is designed to provide an API similar to the server-side PHP translation system.

Some of the tools provided by drupal.js cater to commonly needed services. I am referring to these as Utility functions.

Finally, Drupal offers a framework for adding scripted behaviors to a page. When the page is loaded, these behaviors are automatically "attached" to the page, and immediately take effect. This framework is called Drupal Behaviors (often shortened to just behaviors). If this all sounds unclear, don't worry. We will look at this topic in more detail later in this chapter.

Now we are ready to move on to a discussion of behaviors.

0 0

Post a comment