I have a style plugin, which admin uses to specify some options.
For example, admin specifies the value of 'myvar': $options['myvar'] = 'abcd';

I also have a javascript file which defines some behaviors associated with this style plugin.
My Question: Is it possible to access $options when defining Drupal.behaviors?

(function ($) {
  Drupal.behaviors.mymodule = {
    attach: function (context, settings) {
      var myvar = $options['myvar']; // How can I access $options?
      ...
    }
  };
})(jQuery);

What is the best way to accomplish this?
I suppose I could call drupal_add_js from within the plugin, to add all the $option variables to Drupal.settings. But I am wondering if there is a better way?

Thanks very much!

Comments

kobnim’s picture

I suppose another possibility might be to move my Drupal-behavior javascript inside one of the class definitions in my custom style plugin, for example inside function pre_render($result)

Would this be a reasonable approach?

If so, do you have any suggestions on which class to use? (I don't need access to $result, I only need access to $this->output, so perhaps pre_render is not the best choice.)

Thanks.

merlinofchaos’s picture

Status: Active » Fixed

I think the drupal_add_js() to add your options to the settings so the javascript can read them is probably the better choice.

kobnim’s picture

thank you!

Status: Fixed » Closed (fixed)

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