将功能添加到插件js中,而无需实际修改js文件 [英] Add function to the plugin js without actually modifying js file
问题描述
I want to add 'setInputDataId' function at same level as the existing function '_select' is, in plugin js http://twitter.github.io/typeahead.js/releases/latest/typeahead.bundle.js file without actually modifying the file itself. So I would like to know how can I add new function, I would appreciate the help.
插件中的现有代码:
_select: function select(datum) {
this.input.setQuery(datum.value);
this.input.setInputValue(datum.value, true);
this._setLanguageDirection();
this.eventBus.trigger("selected", datum.raw, datum.datasetName);
this.dropdown.close();
_.defer(_.bind(this.dropdown.empty, this.dropdown));
},
我想要的新代码:
_select: function select(datum) {
this.input.setQuery(datum.value);
this.input.setInputValue(datum.value, true);
this.input.setInputDataId(datum.raw);
this._setLanguageDirection();
this.eventBus.trigger("selected", datum.raw, datum.datasetName);
this.dropdown.close();
_.defer(_.bind(this.dropdown.empty, this.dropdown));
},
而且由于'setInputDataId'函数最初不在插件中,因此我也想添加此函数.
And also as 'setInputDataId' function is not originally in plugin, I want to add this function too.
功能主体为:
setInputDataId: function setInputDataId(raw) {
this.$input.attr('data-id',raw.id);
},
请转到 http://twitter.github.io/typeahead.js/examples/ 在chrome的控制台标签中执行以下操作:
Please goto http://twitter.github.io/typeahead.js/examples/ In chrome's console tab do this:
console.dir($.fn.typeahead)
现在扩展节点功能,然后是",然后是第二个Closure,在那里您可以看到Typeahead.现在单击Typeahead原型,您可以在其中看到_select方法.如何修改此功能.
Now expand node function then '' then second Closure there you can see Typeahead. now click on Typeahead the prototype there you can see _select method. How can I modify this function.
层次结构:
function
<function scope>
Closure (second)
Typeahead
prototype
_select
推荐答案
由于_只是一个对象,只需在添加上述文件后将其添加到脚本文件中即可.
As _ is just an object simply add it to your script file following the addition of the above file.
例如:
_.functionName = function() { //implementation };
首先,请打开控制台并在那里进行尝试.
First off just open up the console and try it there.
输入_并按回车键,应为您提供对象内容.然后尝试上面的伪代码,然后开始吧!但是很明显,除非您将其添加到文件中的某个位置,否则该文件将一直存在,直到关闭浏览器.
Enter _ and hit enter, you should be given the objects contents. Then try the above pseudo code and off you go! But obviously unless you add it to a file somewhere it will only live until you close the browser.
这篇关于将功能添加到插件js中,而无需实际修改js文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!