$(document).ready(function() { $("input[name=search-query]").typeahead({ minLength: 2, source: function (query,process) { if(query.length==0) return false; search('u/search/ajaxlist',query,process); }, matcher: function () { return true; }, updater: function (item) { window.parent.location.href = site_url+users[item]; }, }); }); var c=0; var search = _.debounce(function(url,query,process){ $.ajax({ url : site_url+url, type : 'GET', data : 'term=' + query, dataType : 'JSON', async : true, cache : false, beforeSend : function() { if (c++ == 0) $('img[name=searching]').css('visibility', 'visible'); }, success : function(data) { // if json is null, means no match, won't do again. if(data==null || (data.length===0)) return; users = {}; userLabels = []; _.each(data,function(item,ix,list) { if (_.contains(users,item.label)) item.label = item.label + ' #' + item.value; userLabels.push(item.label); users[item.label] = item.value; }); process(userLabels); }, complete : function() { if (--c == 0) $('img[name=searching]').css('visibility', 'hidden'); } }) }, 500);