-
-
Save hellsingblack/4252843 to your computer and use it in GitHub Desktop.
Bootstrap Typeahead + Backbone.js
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
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