【问题标题】:How to extend a library like bootstrap-typeahead.js如何扩展像 bootstrap-typeahead.js 这样的库
【发布时间】:2012-08-25 08:34:26
【问题描述】:

假设我想在原来的 bootstrap-typeahead.js 中添加一些功能。
我不喜欢分叉另一个库然后对其进行编辑以添加功能或使用分叉库的想法。

一个用例可能是:将选项 onselect 添加到 $.fn.typeahead.defaults 以便在我选择自动完成中的项目时执行回调。

这是我的测试(1),请添加 cmets 和代码使其工作。


(1)

_.extend($.fn.typeahead.defaults, {onselect: null});

element.typeahead({
    minLength: 3,
    source: function () {
        return ['val1', 'val2'];
    },
    onselect: function (val) {
      console.log(val); // it should be able to get the value selected  
    }
});

【问题讨论】:

    标签: javascript jquery autocomplete twitter-bootstrap


    【解决方案1】:

    JavaScript 是一种非常动态的语言,它允许您在运行时轻松地重新定义或修改函数。示例见this answer of mine

    但是,通过链接到分叉的 typeahead 以提供 onselect 回调,似乎有人已经拥有 answered your question

    【讨论】:

    • +1 感谢您的回答。正如我所说,我会避免使用分叉的预输入。说我应该重新定义Typeahead.prototype.select以使用this.onselect。知道怎么做吗?
    • 很多关于如何制作的想法(:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-03
    • 2017-04-25
    • 2013-07-26
    • 1970-01-01
    • 1970-01-01
    • 2014-09-10
    • 2017-01-10
    相关资源
    最近更新 更多