class window.GlobalSearch extends Backbone.View events: 'keyup #global-search': 'search' initialize: (options = {}) -> @$field = $("#global-search") @$results = $("#global-search-results") @$results.css right: @$field.offset().left - @$field.outerWidth() - @$results.outerWidth() search: (event) -> value = $.trim @$field.val() return @clear() if value.length == 0 return if value.length < 2 return if value == @last_value @last_value = @$field.val() @run_search() # @interval = setInterval(@run_search, 300) if !@interval clear: -> @$results.addClass("hide") clearInterval(@interval) @interval = undefined @last_search = undefined @last_value = "" @$el.removeClass("clear").removeClass("loading") @$results.empty() @$field.val("").focus() run_search: -> query = $.trim @$field.val() return @clear() if query == "" return if query == @last_search @last_search = query @loading.abort() if @loading @$el.addClass("loading").removeClass("clear") @$results.removeClass("hide") console.log "running for: #{query}" @loading = $.ajax url: '/search' data: { term: query } success: (data) => @$results.html(data) clearInterval(@interval) @interval = undefined @$el.removeClass("loading").addClass("clear") @loading = undefined dataType: 'html' type: 'get' # $ -> # $("#global-search").autocomplete # source: "/search" # appendTo: "#global-autocomplete-results" # minLength: 2 # select: ( event, ui ) -> # response = if ui.item # console.log ui.item # "Selected: " + ui.item.value + " aka " + ui.item.id # else # "Nothing selected, input was " + this.value # console.log response # $("#global-search").data("ui-autocomplete")._renderItem = ( ul, item ) -> # console.log item # $( "