JavaScript in Drupal 7: What developers need to knowPresentation Transcript
JavaScript in Drupal 7:
WHAT DEVELOPERS NEED TO KNOW
October 24th, 2009
PROBLEM:
jQuery’s use of the $ can interfere with other
libraries.
D6:
$(document).ready(function(){
....
});
October 24th, 2009
SOLUTION:
Wrap everthing in the following:
(function($) {
....
})(jQuery);
October 24th, 2009
PROBLEM:
Not enough flexibility in how we can add js to our Drupal pages:
- can’t fully control the ordering of js files
- can’t load external js
D6:
drupal_add_js(‘path/to/some_js_file.js’, ‘module’, ‘header’);
October 24th, 2009
SOLUTION:
drupal_add_js() now allows you to add a weight value to each script
you’re adding:
- JS_LIBRARY: Any libraries, settings, or jQuery plugins.
- JS_DEFAULT: Any module-layer JavaScript.
- JS_THEME: Any theme-layer JavaScript.
drupal_add_js('jQuery(document).ready(function ()
{ alert("Hello!"); });', array('type' => 'inline', 'scope' => 'footer',
'weight' => 5);
October 24th, 2009
SOLUTION:
drupal_add_js() allows you to load external js files:
drupal_add_js('http://example.com/example.js', 'external');
October 24th, 2009
PROBLEM:
Some JavaScript code is being provided by core or some contrib
module but it’s not the most up-to-date version.
October 24th, 2009
SOLUTION:
hook_js_alter()
function hook_js_alter(&$javascript) {
// Swap out jQuery to use an updated version of the library.
$javascript['misc/jquery.js']['data'] = drupal_get_path('module',
'jquery_update') . '/jquery.js';
}
October 24th, 2009
AJAX in D7.... :-)
function quicktabs_ajax($form, $form_state) {
$form_tabs = $form['qt_wrapper']['tabs'];
$output = drupal_render($form_tabs);
return $output;
}
October 24th, 2009
SOLUTION:
There’s now a framework for ajax in Drupal
Merlinofchaos has done all the hard work, so you don’t have to! :-)
October 24th, 2009
PROBLEM:
Ajax-loaded content doesn’t get behaviors attached to it if they
depend on Drupal.settings
October 24th, 2009
SOLUTION:
Drupal.attachBehaviors now takes a second parameter, which is the
local settings for the portion of the DOM it’s attaching behaviors to:
Drupal.attachBehaviors(context, settings)
October 24th, 2009
To have the settings available for your ajax-loaded content:
- your ajax callback must make a call to drupal_add_js to grab the
JavaScript for the output it’s rendering
- it must return the settings array as part of its response
- when your ajax success function makes a call to
Drupal.attachBehaviors, it must pass in the settings from the
response.
October 24th, 2009
jQuery UI is in core
function render_accordion_block() {
$build['myelement'] = array(
'#theme' => 'my_accordion',
);
$build['myelement']['#attached']['library'][] = array('system',
'ui.accordion');
$build['myelement']['#attached']['js'][] = array('data' =>
'(function($){$(function() { $("#accordion").accordion(); })})(jQuery);',
'type' => 'inline');
$output = drupal_render($build);
return $output;
}
October 24th, 2009
Miscellaneous Changes:
- Drupal.behaviors are now attached and detached
- drupal_to_js is now drupal_json_encode
- drupal_json is now drupal_json_output
- use jQuery.once() to attach a behaviour once
October 24th, 2009
QUESTIONS?
October 24th, 2009
Let LinkedIn power your SlideShare experience
+
Let LinkedIn power your SlideShare experience
Customize SlideShare content based on your interests
We will import your LinkedIn profile and you will be visible on SlideShare.
Keep up to date when your LinkedIn contacts post on SlideShare
1–1 of 1 previous next