Where should new functions go?
When you need to create a new function in your script (just the code in your Drupal.behaviors child function won't cut it), where in terms of namespace should that function go? Is there an official decision or consensus on this?
In practice, I've been placing them in the Drupal.settings.myModule object, alongside the settings variables. It seems like an odd place to put it, but it works. The caveat is that since this object isn't created until after the inline JS that Drupal puts inline is executed, that namespace doesn't exist. So I have to create the function inside my Drupal.behaviors function.
Drupal.behaviors.myModule = function(context) {
if (!$('body').hasClass('myModule-processed')) {
Drupal.settings.myModule.newFunction = function() {
// This will work.
};
}
}
Drupal.settings.myModule.newFunction = function() {
// This will cause an error since Drupal.settings.myModule doesn't
// exist yet.
}Does this make sense? How do you go about it?

New per module functions in Drupal object itself
// Note that the Drupal object already exists since it's loaded before your .js file.
// Now, this stmt ensures your object exists prior to adding more methods and/or properties.
Drupal.myModule = Drupal.myModule || {};
/**
* My function description.
*/
Drupal.myModule.newFunction = function() {
}
Drupal.settings is just for passing data from PHP to your javascript behavior using drupal_add_js(..., 'setting').
HTH :)
That's a good solution, but
That's a good solution, but how official is that? Is that just your opinion, or the for-sure way of doing things, or somewhere in between…?
Maybe there needs to be a Drupal JavaScript ways and means doc sprint…
That's about as official a
That's about as official a solution as there is ;-)
Don't reinvent the wheel, look at how core .js files do ;-)
There is always more than one way to do it, but if you don't wish to reinvent the wheel, look at several examples in drupal's /misc folder.
You may also wish to take a look at the handbooks:
http://drupal.org/node/121997
Cheers