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
amphetamachine
  • 27,620
  • 12
  • 60
  • 72
vinayakj
  • 5,591
  • 3
  • 28
  • 48
  • I have found the answer at http://stackoverflow.com/a/4087423/3462952 & http://stackoverflow.com/a/2051280/3462952 – vinayakj Apr 21 '14 at 19:10

1 Answers1

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.

Sten Muchow
  • 6,623
  • 4
  • 36
  • 46
  • 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. – vinayakj Apr 21 '14 at 16:19
  • how about some pseudo code so i can get a better idea of what you would like to do? – Sten Muchow Apr 21 '14 at 16:21
  • http://twitter.github.io/typeahead.js/releases/latest/typeahead.bundle.js 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. – vinayakj Apr 21 '14 at 16:26
  • 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 '' node – vinayakj Apr 21 '14 at 16:34
  • Hi Sten.. I have modified post.. now you can see what I exactly want to do. – vinayakj Apr 21 '14 at 16:46
  • can you please give me code how to modify _select function – vinayakj Apr 21 '14 at 18:26
  • I have edited post for exact hierarchy of _select function. can you now give me the code please. – vinayakj Apr 21 '14 at 18:40
  • modify the prototype of the _select function and add your code to the prototype chain as well. http://stackoverflow.com/questions/7396417/javascript-modify-prototype-of-an-objects-variable – Sten Muchow Apr 21 '14 at 19:08