Having issues with Twitter typeahead bloodhound tokens not functioning properly

Why are the tokens not working? I expect 'Inkoopverenigingen' to be suggested when typing 'east', 'home', or 'trap'.

json:

[
{
    "value": "Inkoopverenigingen",
    "tokens": [
        "inkoopverenigingen",
        "inwoopverenigingen",
        "east",
        "home",
        "trap"
    ]
},
{
    "value": "Aannemers - Afbraakwerken",
    "tokens": [
        "West",
        "Side",
        "Story"
    ]
},
{
    "value": "Affichageondernemingen",
    "tokens": [
        "West",
        "Side",
        "Story"
    ]
},
{
    "value": "Aanwervingsbureaus",
    "tokens": [
        "West",
        "Side",
        "Story"
    ]
}

]

javascript:


    var engine = new Bloodhound({
    datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    limit: 15,
    prefetch: 'data/typeahead/what.json',
    remote: 'data/typeahead/queries/%QUERY.json'
});

engine.initialize();
engine.clearPrefetchCache();

var template_source = $("#entry-template-what").html();

$('.test .typeahead-what').typeahead({
        hint: true,
        highlight: true,
        minLength: 1
    },
    {
        name: 'what',
        displayKey: 'value',
        source: engine.ttAdapter(),
        templates: {
            empty: no_results,
            suggestion: Handlebars.compile(template_source)
        }
    }
);

html:

<div class="test">
                        <input type="text" class="typeahead-what form-control" id="section" name="section">
                    </div>

html template:

<script id="entry-template-what" type="text/x-handlebars-template">
<div>
    <strong>{{value}}</strong>
    <div class="controls pull-right">
        <span class="glyphicon glyphicon-plus"></span>
        <span class="glyphicon glyphicon-minus"></span>
    </div>
</div>

Answer №1

here is the correct answer:

        datumTokenizer: function(data) { return Bloodhound.tokenizers.whitespace(data.tokens.join(' ')); },

Answer №2

It's worth noting that calling engine.clearPrefetchCache(); is crucial when the JSON file has been updated in the meantime.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Angular implementation of checkboxes to streamline data filtering

I am currently displaying FreeEvents using a checkbox and passing the value to the filter as filter:filterFreeEvent, which is working perfectly fine. However, I would like to avoid passing the value in the filter and instead use a change event of a checkb ...

Retrieve various elements using a loop and dynamic identifiers (perhaps within a multi-dimensional array)

Apologies for the confusing title. I am currently working on a piece of code which can be found here: https://jsfiddle.net/8ers54s9/13/ This code creates a basic text area and compares it to a predefined array to identify common words within a string. If ...

Error: Attempting to access the 'SearchBox' property of an undefined variable is not allowed

I have been working on a Google Maps code that displays both public and private schools with different markers for each category. However, when running the code, I encountered an error specifically on this line of code: var searchBox = new google.maps.pl ...

Code for asynchronous routing in Node.js

My current setup involves using node with express version 4.0, and I've noticed a lack of information online (including in the documentation) regarding embedding asynchronous code within a route. With middleware, it's straightforward: app.use(& ...

Dynamically updating fields in PHP using jQuery's passed variable

In my web application, I have a selection of locker numbers available in a dropdown list that is populated from MYSQL/PHP. My goal is to display each locker's combination and location when a user selects a locker number from the dropdown list on the s ...

Differences between the http module and Express framework

After noticing this common pattern, I've become intrigued : const server = http.createServer(app); // Listen on provided port, on all network interfaces. server.listen(port); server.on('error', onError); server.on('listening', on ...

Ways to render component solely based on the alteration of the class props

In my ReactJS project, I am fetching JSON data from a RESTful Django host and using it to create a table with filters. Here is my Table class: class MainTable extends React.Component { constructor(props) { super(props); this.state = { res ...

A comparison of parent and child components

To implement a child-parent component relationship in Angular, first create two JSON files: parent.json and child.json. The parent.json file should contain the following data: "Id":10001, "name":"John" "Id":10002, ...

Preventing a variable from executing as a function in JavaScript

My service is connected to a MYSQL database, and I am retrieving the count of entries in a table. However, when I try to use result.COUNT(*) as the returned value from the server, it appears to be attempting to execute a function with the parameter *. ...

Guide on marking and unmarking all checkbox options within a specified div

I need a script that will uncheck all checkboxes within the 'test' div except for those that have the disabled attribute. <div id="test"> <input type="checkbox" id="check1" name="check1"> <input type="checkbox" id="check01" ...

Typescript: Defining the correct return type for resolved parameters in promises

Exploring the world of TypeScript, I recently attempted to convert some basic JavaScript promise samples into TypeScript promises. While working on this conversion process, I encountered an issue that has left me puzzled and unable to find a solution even ...

Does using breakpoints in strict mode affect the outcome?

Here is the code snippet: 'use strict'; var foo=function(){ alert(this); } var bar = { baz:foo, }; var x = bar.baz; x();//1 After executing the code directly, everything works fine and it alerts undefined. However, when I insert a break ...

What is the best way to send messages to both my queue and make a post request?

Currently, I have a function in place that successfully posts data to the database. However, I am now trying to enhance this function by also incorporating sending a message to trigger another function simultaneously. I attempted to implement both sending ...

Dealing with a caller disconnecting the call before the recipient has a chance to grant media permission in WebRTC

Encountering an issue with the local stream from the camera. The process unfolds as follows: The caller initiates a call. A "Disconnect" button becomes visible for the caller. The CALLEE is prompted by the browser to grant permission. The CALLER clicks o ...

What is the best way to store a file object in React's state?

I am currently working on setting the state for a file object that is being passed between modals before it is uploaded to the server. Below is the code snippet that demonstrates what I am attempting to achieve. const initialState = { selectedD ...

When attempting to utilize POST commands in a React application, encountering the error "Cannot GET /"

I am encountering an issue with my react app - when I run node index.js, I receive a "Cannot GET /" error directly on my browser (even though I want to run it directly), despite requesting a POST command. Previously, the GET request was working fine. Curr ...

What is the best way to showcase a modal component when the page loads?

Currently, I am working on an HTML5 game page and I am looking to create a modal login form that appears when the user first loads the page. I have attempted to set the state to true and even tried transferring the state to app.js, but unfortunately, neith ...

Removing the cloned element is not possible

In my grid, there is a feature where hovering over a box creates a clone of that box which is positioned directly above it (using z-index/overlay). Once the user moves the cursor away from the box, an animation should play, and at the end of it, the box sh ...

Searching through Mongoose using various fields, some of which are optional

Recently, I've delved into the world of mongoose and am currently working on an app to enhance my learning. My project involves an Artist Schema and a search form with multiple optional fields. For example, users can input a name, minimum age, and sea ...

What is the best way to create a flip page effect in a book without relying on absolute positioning? Alternatively, how can a relatively positioned div adjust to the size

Check out this Codepen I have created a div that simulates the pages of a book, with front and back elements. When you click on the page, it flips to reveal the reverse side. To style the book properly, I set the parent div (which houses the page divs) t ...