【问题标题】:Monkey Patch Chosen jQuery library - Chosen always undefinedMonkey Patch Chosen jQuery 库 - Chosen 始终未定义
【发布时间】:2013-01-14 06:04:32
【问题描述】:

我正在尝试修补 Chosen jQuery 库中的两个函数,但是,无论我尝试引用什么原始函数,控制台返回的 Chosen 都是未定义的。

这是我的代码。

        function(){
        var _no_results = window.jQuery.fn.chosen.prototype.no_results;
        var _no_results_clear = window.jQuery.fn.chosen.prototype.no_results_clear;

        window.jQuery.fn.chosen.prototype.no_results = function (terms) {
            console.log('nr');
            var no_results_html;
            no_results_html = $('<li class="no-results">' + this.results_none_found + ' "<span></span>"</li>');
            no_results_html.find("span").first().html(terms);
            if (this.options.no_results_callback) {
                this.options.no_results_callback(no_results_html, terms);
            }
            return this.search_results.append(no_results_html);
            //return _no_results.apply(terms);
        }


        window.jQuery.fn.chosen.prototype.no_results_clear = function (terms) {
            console.log('nrc');
            var no_results_html;
            no_results_html = $('<li class="no-results">' + this.results_none_found + ' "<span></span>"</li>');
            no_results_html.find("span").first().html(terms);
            if (this.options.no_results_clear_callback) {
                this.options.no_results_clear_callback(no_results_html, terms);
            }
            return this.search_results.find(".no-results").remove();
            //return _no_results_clear.apply(terms);
        }
    }

有什么想法吗?

【问题讨论】:

    标签: jquery monkeypatching jquery-chosen


    【解决方案1】:

    似乎所选库的旧版本(例如 v0.9.14)曾经公开 ChosenAbstractChosen,但最新版本(截至 2019 年 3 月 25 日的 v1.8.7)都没有公开。相反,当前版本利用.data(key, value) jQuery methodchosen 键下存储对Chosen 对象的引用。您可以尝试将其与初始化的选择列表和 Object.getPrototypeOf JavaScript method 结合使用以覆盖 ChosenAbstractChosen 函数。

    例如,假设您页面上的某处有:

    <select id="futureChosenSelect" ... >
      <option value="HiMom">Hi Mom!</option>
      ...
    </select>
    

    你已经配置了 &lt;select&gt; 并选择了:

    $('#futureChosenSelect').chosen();
    

    您应该能够使用以下内容覆盖函数:

    Object.getPrototypeOf( $('#futureChosenSelect').data('chosen') ).no_results = function(terms) { ... };
    

    【讨论】:

      【解决方案2】:

      不要复制原型,这不是 jQuery 的工作方式。在内部,它将插件和变量存储在映射中(名称-> 函数)。这应该有效:

       var _no_results = window.jQuery.jQuery.fn['chosen'].no_results;
       var _no_results_clear = window.jQuery.fn['chosen'].no_results_clear;
      
       window.jQuery.fn['chosen'].no_results = function (terms) { 
          ...
          _no_results.apply(this, terms);
       }
      
       window.jQuery.fn['chosen'].no_results_clear = function (terms) { 
          ...
          _no_results_clear.apply(this, terms);
       }
      

      我曾经这样做是为了替换选择器链内部日志的 jQuery 内部函数。 Check here how it works (github)

      【讨论】:

      • 不幸的是 _no_results 仍然返回为未定义。
      • @ScampDoodle 我最初添加了错误的代码,我已经更改了它。
      • 刚刚检查了代码,您无法访问它,因为它们是私有变量,包含在自运行函数中。如果不直接更改内部代码,您将无法更改它。充其量您可以覆盖 jQuery.jQuery.fn['chosen'],捕获 {no_results_text: "No results match"} 参数,然后运行您的代码,然后将其应用于所选的原始函数
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多