0

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

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

8 Comments

Hi Sten, thanks.. but I want the new function as member function of plugin bcoz i would also be passing some parameters from '_select' function of plugin.. do u know how can do this.
how about some pseudo code so i can get a better idea of what you would like to do?
twitter.github.io/typeahead.js/releases/latest/… in this js search for function '_select'. I want to call a 'setInputDataId' function from this function, so first i need to add 'setInputDataId' at same level as '_select' function is.
Thanks.. I did but I am confused.. cant find exact location..When I console.dir the Typeahead object to see the hierarchy, I am unable to track exact location of the '_select' function. The console in chrome shows the function under '<function>' node
Hi Sten.. I have modified post.. now you can see what I exactly want to do.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.