Download & Extend

Pass Settings Locally to JS Behaviors

Project:Drupal core
Version:7.x-dev
Component:javascript
Category:feature request
Priority:normal
Assigned:quicksketch
Status:closed (fixed)
Issue tags:JavaScript

Issue Summary

In working with JavaScript in Drupal, we've run into several problems with dynamically loaded data. This patch is meant to solve exactly one of those problems. Currently when we load content via AJAX or AHAH, we only get the needed HTML. We don't get any JS or CSS that is required by the new content, we have to assume that it's already there on the original page.

This patch addresses a subset of that problem. When we attempt to attach behaviors to the that new content, often times it depends on values in the Drupal.variables array (such as when using teaser.js, ahah.js, or tabledrag.js). The problem is that all these scripts call the Drupal.settings array directly. So other than editing this global JavaScript variable, there isn't a way to make these scripts work. So instead we should switch these variables to local ones, so we can pass in any values we like.

Of course this patch doesn't do much on it's own, but future enhancements (after #251578: More flexible js/css ordering and an alter operation and subpatches are finished) should allow us to send back not just HTML on AHAH requests, but also JS, CSS, and Drupal.settings back to the calling page to be loaded dynamically.

This patch would be a pre-requisite to clean Popups functionality, we have a workaround in the Popups API project (see #336641: Auto-load needed JS and CSS files (and settings)), but this would make it so that we could effectively apply behaviors that depend on the Drupal.settings variables.

AttachmentSizeStatusTest resultOperations
drupal_behavior_settings.patch24.53 KBIdleFailed: Invalid PHP syntax.View details

Comments

#1

Your local .project file got in there somehow

#2

Status:needs review» needs work

The last submitted patch failed testing.

#3

Status:needs work» needs review

Hrm. Same patch, no ".project" file this time.

AttachmentSizeStatusTest resultOperations
drupal_behavior_settings.patch23.61 KBIdleFailed: Failed to apply patch.View details

#4

subscribing

#5

Status:needs review» needs work

The last submitted patch failed testing.

#6

Status:needs work» needs review

Reroll for fuzz.

AttachmentSizeStatusTest resultOperations
drupal_behavior_settings.patch23.55 KBIdleFailed: Failed to apply patch.View details

#7

Status:needs review» needs work

The last submitted patch failed testing.

#8

Subscribing!

#9

#10

AttachmentSizeStatusTest resultOperations
drupal_dehavior_settings_10.patch23.47 KBIdlePassed: 10551 passes, 0 fails, 0 exceptionsView details

#11

Status:needs work» reviewed & tested by the community

The patch looks good. Manually tested across the JS and it works.

The patch in #10 just accounts for a moving head.

#12

This is a smart change to make.

#13

This is awesome. I just tested it out properly with Quick Tabs and was able to make an ajax-loaded QT react to settings that didn't exist on the original page - it would be great to see this get in.

#14

Somehow my PHPdoc got dropped from #11. Same patch with docs.

AttachmentSizeStatusTest resultOperations
drupal_behavior_settings.patch22.99 KBIdlePassed: 10551 passes, 0 fails, 0 exceptionsView details

#15

I'm positive #14 and #10 are identical, though I'm sure why the filesize got smaller rather than bigger. If preferred, you can use this patch in conjunction with #10 to add the docs.

AttachmentSizeStatusTest resultOperations
drupal_dehavior_settings_docs.patch642 bytesIdlePassed: 10551 passes, 0 fails, 0 exceptionsView details

#16

Sorry, but wrong indentation here:

+ * @param settings
+ *  An object containing settings for the current context. If none given, the
+ *  global Drupal.settings object is used.

I do not want to change the status because of this. Injecting 2 spaces shouldn't be too much for any core maintainer. ;)

#17

Status:reviewed & tested by the community» needs work
Issue tags:+Needs Documentation

I looked this over at the ice cream sprint and the only issue I found was the PHPDoc. That's now fixed, so I committed this to HEAD. Thanks!

Please document. :)

#18

Status:needs work» fixed
Issue tags:-Needs Documentation

Added documentation

#19

Status:fixed» closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.