I'm trying to use/configure typeahead to just perform the ajax call if the typed string contains space. To be more specific, I would like to display fulladdresses but at my API I would like to avoid the street number part. I was trying to achieve that using a function assigned to the 'source' property:
$("#select").typeahead({
minLength: 3,
source: function (request, response) {
var streetName = '';
for (i = 1; i < streetParts.length; i++) {
streetName += streetParts[i] + ' ';
}
if (streetName.length < 3) return null;
$.ajax({
url: "/api/Search/suggest/",
data: { "input": streetName.trim() },
type: "GET",
hint: true,
contentType: "json",
success: function (data) {
items = [];
map = {};
$.each(data, function (i, item) {
var id = item.document.id;
var name = item.text;
map[name] = { id: id, name: name };
items.push(name);
});
response(items);
},
error: function (response) {
alert(response.responseText);
},
failure: function (response) {
alert(response.responseText);
}
});
},
updater: function (item) {
//If simultaneously want to update value somewhere else
$("#selected").html(map[item].id);
return item;
}
});
even though I can see the request at the network tab is fine, the div with the results are not being rendered. If I remove the if statement from the source function it works fine. Is it the right way to cancel the execution of the source function (returning null)? Is there any other property or way to achieve what I'm trying?