Typeahead.js - render event - do not receive an array of sentences in one argument

I am trying to use the typeahead render event but cannot pass arguments correctly.

Referring to https://github.com/twitter/typeahead.js/blob/master/doc/jquery_typeahead.md#custom-events , the rendering event must go through 4 arguments.

I set my type and header handler as follows:

         $('#input').typeahead({
         hint: true,
         highlight: true,
         minLength: 1
     },
 {
     name: 'items',
     source: items
 })
 .on('typeahead:render', onRender);

 function onRender($event, $suggestions, $async, $dataSet)
         {
}

Render event files, if they are expected but do not pass arguments correctly.

$ event - The jQuery event object as specified. But I would expect the second argument of the $ sentence to be an array containing the current sentences, but it contains only the first sentence. The next two arguments contain the 2nd and 3rd sentences, not the async flag and dataset name, as expected.

, . , .

var substringMatcher = function(strs) {
  return function findMatches(q, cb) {
    var matches, substringRegex;

    // an array that will be populated with substring matches
    matches = [];

    // regex used to determine if a string contains the substring `q`
    substrRegex = new RegExp(q, 'i');

    // iterate through the pool of strings and for any string that
    // contains the substring `q`, add it to the `matches` array
    $.each(strs, function(i, str) {
      if (substrRegex.test(str)) {
        matches.push(str);
      }
    });

    cb(matches);
  };
};

var states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California',
  'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii',
  'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana',
  'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota',
  'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire',
  'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota',
  'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island',
  'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont',
  'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'
];

console.log("starting");
$('#the-basics .typeahead').typeahead({
  hint: true,
  highlight: true,
  minLength: 1
},
{
  name: 'states',
  source: substringMatcher(states)
})
.on('typeahead:render', onRender);

   function onRender($event, $suggestions, $async, $dataSet)
         {
           console.log($event);
             console.log($suggestions);
             console.log($async);
             console.log($dataSet);
           
           }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://twitter.imtqy.com/typeahead.js/releases/latest/typeahead.bundle.js"></script>

<div id="the-basics">
<input type="text"  class="typeahead" />
  </div>
+4
1

,

x.bind('typeahead:render',
            function (ev) {
                var suggestions = Array.prototype.slice.call(arguments, 1);
            }
        );

async datasets

+3

Source: https://habr.com/ru/post/1615557/


All Articles