Ajax' like autocomplete for Meteor & Bootstrap

28 Jan 2013

Straightforward solution for populate Twitter Bootstrap typeahead source data in Meteor.

On client side:

Template.items.rendered = ->
  $('input#item').typeahead
    source: (query, process) ->
      subscription = Meteor.subscribe 'autocompleteItems', query, ->
        process _(Items.find().fetch()).pluck('name')
      subscription.stop() # here may be a bit different logic,
      # such as keeping all opened subsriptions until autocomplete
      # will be successfully completed and so on
    items: 5

On server side:

Meteor.publish 'autocompleteItems', (query) ->
  Items.find
    name: new RegExp(query, 'i'),
      fields: { name: 1 },
      limit: 5

comments powered by Disqus