Skip to content

Instantly share code, notes, and snippets.

@billyvg
Created May 31, 2012 00:53
Show Gist options
  • Save billyvg/2839956 to your computer and use it in GitHub Desktop.
Save billyvg/2839956 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