2

I'm trying to make some of my Javascript in a Drupal 7 module a bit more modular, as I want to access a function inside Drupal.behaviors.attach() from another JS file. So first of all I need to move some variable declarations, and then the functions, outside of the attach function and hopefully access the variables from inside it with this.vars.myVariable - I'll try and explain a bit further with some slightly dummified code...

Initial, working code:

(function($) {
  Drupal.behaviors.myModule_someJS = {

    attach: function(context, settings) {

      var initial_page = Drupal.settings.myModule.initial_page;
      console.log(initial_page); // returns a value

    }
  }
})(jQuery);

However, this doesn't work:

(function($) {
  Drupal.behaviors.myModule_someJS = {

    vars: {
      initial_page : Drupal.settings.myModule.initial_page
      // Uncaught TypeError: Cannot read property 'initial_page' of undefined 
    },

    attach: function(context, settings) {

      console.log(this.vars.initial_page);

    }
  }
})(jQuery);

It seems that outside of the scope of the attach function, Drupal.settings doesn't exist. Is there any way I can access these settings (defined in my .module file) from anywhere within behaviors.myModule_someJS ?

Thanks! Matt

7
  • I don't see where your error is, but Drupal.settings is definitely available outside the scope of the attach function. Just open FireBug console on a Drupal 7 site and type Drupal.settings - you will see that the object exists. Commented Mar 5, 2013 at 16:33
  • Unless anyone knows how I can access a function within the attach function from a separate javascript file? Because that's what I'm essentially trying to do, but it seems impossible, unless I'm just an idiot... Commented Mar 5, 2013 at 16:34
  • @AliceHeaton, thanks for your response! If I type Drupal.settings.myModule.initial_page into the console it gives my answer, but outside of the attach it doesn't seem to... Commented Mar 5, 2013 at 16:37
  • To make a function visible outside the scope, just make an assignment to an attribute of window, eg. window.myfunction = myfunction. Not recommended and may cause all sorts of new scoping issues, mind. Commented Mar 5, 2013 at 16:43
  • Thanks guys, worked it out myself. Until attach() is called, Drupal.settings doesn't contain my custom variables. I'll answer my own question later (not enough rep to do it before 8 hours! Bah..) Commented Mar 5, 2013 at 17:08

1 Answer 1

1

Worked it out myself.

(function($) {
  Drupal.behaviors.myModule_someJS = {

    anotherFunction: function() {
      console.log(this.vars.initial_page);
    },

    attach: function(context, settings) {

      this.vars = {
        initial_page  : Drupal.settings.myModule.initial_page
      }

      this.anotherFunction();

    }
  }
})(jQuery);

Drupal.settings won't contain the custom stuff sent through from my module until the attach function is called, so therefore every settings variable can only be called inside attach()

(and from Alice's comments above, it may be a matter of drupal_add_js being called from within the page rather than in the init() hook)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.