如何在运行时动态重新配置Drupal的基于jQuery的自动完成?

mik*_*ikl 5 javascript jquery drupal autocomplete drupal-6

Drupal有一个非常好的架构,基于jQueryautocomplete.js.通常,您不必费心,因为它的配置和执行由Drupal表单API处理.

现在,我需要一种在运行时重新配置它的方法(使用JavaScript,即).我有一个标准下拉选择框,旁边有一个文本字段,根据选择框中选择的选项,我需要调用不同的URL进行自动完成,其中一个选项应该完全禁用自动完成.是否可以重新配置现有的自动完成实例,还是我必须以某种方式销毁和重新创建?

mik*_*ikl 3

好吧,作为参考,我整理了一个可行的技巧,但如果有人能想到更好的解决方案,我会很高兴听到。

Drupal.behaviors.dingCampaignRules = function () {
  $('#campaign-rules')
    .find('.campaign-rule-wrap')
      .each(function (i) {
          var type = $(this).find('select').val();

          $(this).find('.form-text')
            // Remove the current autocomplete bindings.
            .unbind()
            // And remove the autocomplete class
            .removeClass('form-autocomplete')
          .end()
          .find('select:not(.dingcampaignrules-processed)')
            .addClass('dingcampaignrules-processed')
            .change(Drupal.behaviors.dingCampaignRules)
          .end();

          if (type == 'page' || type == 'library' || type == 'taxonomy') {
            $(this).find('input.autocomplete')
              .removeClass('autocomplete-processed')
              .val(Drupal.settings.dingCampaignRules.autocompleteUrl + type)
            .end()
            .find('.form-text')
              .addClass('form-autocomplete');
            Drupal.behaviors.autocomplete(this);
          }
      });
};
Run Code Online (Sandbox Code Playgroud)

此代码来自ding_campaign 模块。如果您需要做类似的事情,请随意查看代码。都是GPL2。