I have several javascript files in custom modules containing many functions which are used across the code. In D6 I simply added them during hook_init() and it was fine.
drupal_add_js("/scripts/jquery.url.js", array('group' => JS_LIBRARY));
drupal_add_js("/scripts/jquery.cookie.js", array('group' => JS_LIBRARY));
drupal_add_js("/scripts/mymod.funcs.js", array('group' => JS_DEFAULT));
...
drupal_add_js("/scripts/otherfuncs.js", array('group' => JS_DEFAULT));
However, in D7, I am now getting the "$ is not a function". Wrapping all my code as described in the D6 to 7 conversion wasnt't seeming to work since I was then getting "<one of my functions> is not defined". Using
var $ = jQuery.noConflict();
at the start of my 'parent' file resolved the error. But that doesn't seem like a good idea either. Below is an example of the javascript.
//in main file: mymod.funcs.js, global functions
function my_storage(key) { return value; }
//in another file: otherfuncs.js
Drupal.behaviors.myMod = { attach: myMod_Splash }
function myMod_Display(splash) { ... }
function myMod_Splash() {
var splash = my_storage('splash');
if (splash) { myMod_Display(splash); }
}
What is wrong when simply wrapping all of it with the recommended? What else should be done, aside from converting the function groups to objects (which still wouldn't necessarily resolve the wrapping problem)?
EDIT: I was wrapping my code like:
//(function ($) { //tried here
Drupal.behaviors.myMod = { attach: myMod_Splash }
(function ($) { //and here
function myMod_Display(splash) { ... }
function myMod_Splash() {
var splash = my_storage('splash');
if (splash) { myMod_Display(splash); }
}
})(jQuery);
UPDATE: I just removed the "var $ = jQuery.noConflict();" from my file to begin messing with the problem again, and am now getting no errors at all, in Web/Error Console or Watchdog. None of the code was wrapped. I was constantly clearing cache in Drupal before so really have no clue what is going on now. Could the JS aggregation still have been placing the files in the wrong group, hence loading in a conflicting order?
$
in your code withjQuery
.//
at the beginning of added sample makes no sense to me. Also, is it wise to attach function not yet defined? Never really worked for me... but maybe it's more of my prejudice than a real problem.