$(function () {
var testObjs = {};
var testNames = [];
//get the data to populate the typeahead (plus an id value)
var throttledRequest = _.debounce(function (query, process) {
//get the data to populate the typeahead (plus an id value)
$.ajax({
url: '< your json url here>',
cache: false,
success: function (data) {
//reset these containers every time the user searches
//because we're potentially getting entirely different results from the api
testObjs = {};
testNames = [];
//Using underscore.js for a functional approach at looping over the returned data.
_.each(data, function (item, ix, list) {
//for each iteration of this loop the "item" argument contains
//1 test object from the array in our json, such as:
// { "id":7, "name":"Pierce Brosnan" }
//add the label to the display array
testNames.push(item.name);
//also store a hashmap so that when bootstrap gives us the selected
//name we can map that back to an id value
testObjs[item.name] = item;
});
//send the array of results to bootstrap for display
process(testNames);
}
});
}, 300);
$(".typeahead").typeahead({
source: function (query, process) {
//here we pass the query (search) and process callback arguments to the throttled function
throttledRequest(query, process);
},
highlighter: function (item) {
var test = testObjs[item];
return '<div class="test">' + '<img src="' + test.photo + '" />' + '<br/><strong>' + test.name + '</strong>' + '</div>';
},
updater: function (selectedName) {
//note that the...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.