简体   繁体   English

如何扩展类似bootstrap-typeahead.js的库

[英]How to extend a library like bootstrap-typeahead.js

Let's suppose I would like to add some functionalities to the original bootstrap-typeahead.js. 假设我想向原始的bootstrap-typeahead.js添加一些功能。
I don't like the idea to fork another library and then edit it to add functionalities or use a forked library. 我不喜欢先派生另一个库,然后对其进行编辑以添加功能或使用派生库的想法。

One use case could be: adding the option onselect to $.fn.typeahead.defaults in order to execute a callback when I select an item in autocomplete. 一个用例可能是:将选项onselect添加到$.fn.typeahead.defaults中,以便在我选择自动完成项时执行回调。

This is my test (1), please add comments and code to make it working. 这是我的测试(1),请添加注释和代码以使其正常运行。


(1) (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 is a very dynamic language which allows you to easily redefine or modify functions at run-time. JavaScript是一种非常动态的语言,使您可以在运行时轻松地重新定义或修改函数。 See this answer of mine for an example. 有关示例,请参见我的答案

However, it would appear that someone has already answered your question by linking to a forked typeahead to provide an onselect callback. 但是,似乎有人已经通过链接到派生的typeahead来提供onselect回调来回答您的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM