Add function to the plugin js without actually modifying js file

╄→гoц情女王★ 提交于 2019-12-25 06:45:10

问题


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.

Existing Code in Plugin:

    _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));
        },

New code I want:

    _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));
        },

And also as 'setInputDataId' function is not originally in plugin, I want to add this function too.

the function body is:

      setInputDataId: function setInputDataId(raw) {
            this.$input.attr('data-id',raw.id);
        },

Please goto http://twitter.github.io/typeahead.js/examples/ In chrome's console tab do this:

console.dir($.fn.typeahead)

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.

Hierarchy:

function
  <function scope>
    Closure (second)
      Typeahead
        prototype
          _select

回答1:


As _ is just an object simply add it to your script file following the addition of the above file.

For example:

_.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.



来源:https://stackoverflow.com/questions/23200678/add-function-to-the-plugin-js-without-actually-modifying-js-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!