Module-noob here. I've seen plenty of threads on this topic in general but haven't found any concerning use of "attach." I'm working on a fairly simple module that applies some JavaScript to every page on the site. It was working fine, but pareview told me not to use drupal_add_js() in hook_page_build(), instructing me to use attach instead. I can't figure out how to get my PHP variables over to my JS using this method.
The JS file itself is loading, but it is not receiving the data from the PHP variables. From what I have read, I understand that the following is the way to accomplish what I am trying to do, but obviously I've got something wrong somewhere. Here is my code:
mymodule.module:
function mymodule_page_build() {
$page['#attached']['js'] = array(
array(
'data' => drupal_get_path('module', 'mymodule') . '/mymodule.js',
'options' => array(
'preprocess' => TRUE,
'every_page' => TRUE,
),
),
array(
'data' => array(
'mymodule_status' => variable_get('mymodule_status', 0),
),' => 'setting',
),
);
}
mymodule.js:
(function ($) {
"use strict";
Drupal.behaviors.myModule = {
attach: function (context, settings) {
var moduleStatus = Drupal.settings.mymodule.mymodule_status;
var milk = 'milk';
$(document).ready(function () {
// Debug:
console.log('first'); // works
console.log(milk); // works
console.log(moduleStatus); // Says mymodule is undefined
}
};
})(jQuery);
Thank you for any help you can offer.