Skip to content

Instantly share code, notes, and snippets.

@hellsingblack
Forked from billyvg/gist:2839956
Created December 10, 2012 19:46
Show Gist options
  • Save hellsingblack/4252843 to your computer and use it in GitHub Desktop.
Save hellsingblack/4252843 to your computer and use it in GitHub Desktop.
Bootstrap Typeahead + Backbone.js
class TypeaheadView extends Backbone.View
tagName: 'input'
initialize: (@options) ->
@source = options.source if options.source?
@
# Retrieves the Backbone Model that matches this view's value
# Will have problems if you have multiple Models with the same attribute that
# you are using as the typeahead's source.
getModel: () ->
# value of the input field
value = @$el.val()
@collection.find (model) =>
source = model.get @source
source is value
# Render the input and call Bootstrap's .typeahead() plugin
render: ->
options = _.extend @options, source: @collection.pluck @source
@$el.typeahead options
@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment