Newer
Older
<script src="/static/js/jquery-2.1.1.min.js"></script>
<script src="/static/js/bootstrap.min.js"></script>
<script src="/static/js/bootstrap3-typeahead.min.js"></script>
<script>
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
var typeaheadTimerId = null;
doTypeahead = function(query, process) {
if (typeaheadTimerId) {
clearTimeout(typeaheadTimerId);
}
typeaheadTimerId = setTimeout(function() {
typeaheadCallback(query, process);
}, 300);
};
var entriesMap = {};
typeaheadCallback = function(query, process) {
var typeaheadEntries = [];
entriesMap = {};
$.ajax({
url: "/suggest",
data: {term: query},
dataType: "json",
success: function(data, status, jqxhr) {
$.each(data, function(idx, entry) {
entriesMap[entry.label] = entry.book_id;
typeaheadEntries.push(entry.label);
});
process(typeaheadEntries);
}
});
};
typeaheadUpdater = function(item) {
//this.$element[0].value = entriesMap[item];
//this.$element[0].form.submit();
var bookUrl = '/book/' + entriesMap[item];
window.location.href = bookUrl;
return item;
};
source: doTypeahead,
updater: typeaheadUpdater,