Drupal Views exposed select list use chosen module


Drupal.behaviors.yourglobal = {
   attach: function(context, settings) {
      var div_id = '#myfield';
      if(typeof settings.chosen !== 'undefined' && $(div_id, context).length > 0) {
              settings.chosen = settings.chosen || Drupal.settings.chosen;

              // Prepare selector and add unwantend selectors.
              var selector = settings.chosen.selector;

              // Function to prepare all the options together for the chosen() call.
              var getElementOptions = function (element) {
                var options = $.extend({}, settings.chosen.options);

                // The width default option is considered the minimum width, so this
                // must be evaluated for every option.
                if ($(element).width() < settings.chosen.minimum_width) {
                  options.width = settings.chosen.minimum_width + 'px';
                }
                else {
                  options.width = $(element).width() + 'px';
                }

                // Some field widgets have cardinality, so we must respect that.
                // @see chosen_pre_render_select()
                if ($(element).attr('multiple') && $(element).data('cardinality')) {
                  options.max_selected_options = $(element).data('cardinality');
                }

                return options;
              };
              $(div_id, context).once('chosen', function() {
                options = getElementOptions(this);
                $(this).chosen(options);
              });
       }
   },
};

Drupal 7 behaviors

behavior主要用于Drupal Ajax前后可以调用. behavior 以 Drupal.behaviors.helloWorld的方式附加上去,helloWorld为一个函数,当页面加载完成以后Drupal会遍历每一个附件上去的函数. 并且会context和settings,context为当前的上下文。settings是Drupal.settings

在Drupal 7中通常的写法为:

Drupal.behaviors.hehe = {
  attach: function(context, settings) {
    // Ajax后或页面加载完成执行.
  },
  detach: function(){context} {
    // Ajax前执行.
  }
};

https://www.drupal.org/update/modules/6/7#drupal_behaviors