4. 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
5. 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
19. 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
20. 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
25. 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
26. 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