Last active
December 23, 2015 04:49
-
-
Save knubie/6582574 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
do -> | |
cache = {} | |
viewKey = 'ttView' | |
methods = | |
initialize: (datasetDefs) -> | |
datasetDefs = utils.isArray(datasetDefs) ? datasetDefs : [datasetDefs] | |
if datasetDefs.length == 0 | |
$.error('no datasets provided') | |
datasets = utils.map datasetDefs, (o) -> | |
dataset = cache[o.name] ? cache[o.name] : new Dataset(o) | |
cache[o.name] = dataset if o.name | |
return dataset | |
return this.each(initialize); | |
initialize = -> | |
$input = $(this) | |
eventBus = new EventBus({ el: $input }) | |
deferreds = utils.map datasets, (dataset) -> | |
return dataset.initialize() | |
$input.data(viewKey, new TypeaheadView( | |
input: $input | |
eventBus: eventBus = new EventBus({ el: $input }) | |
datasets: datasets | |
)) | |
$.when.apply($, deferreds).always -> | |
# deferring to make it possible to attach a listener | |
# for typeahead:initialized after calling jQuery#typeahead | |
utils.defer -> eventBus.trigger('initialized') | |
destroy: -> | |
return this.each(destroy) | |
destroy = -> | |
$this = $(this) | |
view = $this.data(viewKey) | |
if view | |
view.destroy() | |
$this.removeData(viewKey) | |
setQuery: (query) -> | |
return this.each(setQuery) | |
setQuery = -> | |
view = $(this).data(viewKey) | |
view && view.setQuery(query) | |
jQuery.fn.typeahead = (method) -> | |
if methods[method] | |
return methods[method].apply(this, [].slice.call(arguments, 1)) | |
else | |
return methods.initialize.apply(this, arguments) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment