sergeyt:typeahead

v0.10.5_13Published 10 years ago

This package has not had recent updates. Please investigate it's current state before committing to using it in your project.

meteor-typeahead Build Status LICENSE meteor package version

Deps Status DevDeps Status

Twitter's typeahead.js autocomplete package, wrapped for Meteor 1.0+. Issue command meteor add sergeyt:typeahead to install the package.

Examples

See demo application in this repository to find more examples.

data-source attribute

1<input class="form-control typeahead" name="team" type="text"
2       placeholder="NBA teams"
3       autocomplete="off" spellcheck="off"
4       data-source="nba"/>
1Nba = new Meteor.Collection("nba");
2
3if (Meteor.isServer){
4	Nba.insert({name:'Boston Celtics'});
5	// fill Nba collection
6}
7
8Template.demo.helpers({
9  nba: function() {
10    return Nba.find().fetch().map(function(it){ return it.name; });
11  }
12});

Multiple datasets

1<input class="form-control typeahead" name="team" type="text"
2       placeholder="NBA and NHL teams"
3       autocomplete="off" spellcheck="off"
4       data-sets="teams"/>
1Template.demo.helpers({
2  teams: function() {
3    return [
4      {
5        name: 'nba-teams',
6        local: function() { return Nba.find().fetch().map(function(it){ return it.name; }); },
7        header: '<h3 class="league-name">NBA Teams</h3>'
8      },
9      {
10        name: 'nhl-teams',
11        local: function() { return Nhl.find().fetch().map(function(it){ return it.name; }); },
12        header: '<h3 class="league-name">NHL Teams</h3>'
13      }
14    ];
15  }
16});

Custom template to render suggestion

1<input class="form-control typeahead" name="repo" type="text"
2       placeholder="open source projects by Twitter"
3       autocomplete="off" spellcheck="off"
4       data-source="repos" data-template="repo"/>
5
6<template name="repo">
7       <p class="repo-language">{{language}}</p>
8       <p class="repo-name">{{name}}</p>
9       <p class="repo-description">{{description}}</p>
10</template>
1Repos = new Meteor.Collection("repos");
2
3if (Meteor.isServer){
4	Meteor.startup(function(){
5		Repos.remove({});
6		// fill repos from private repos.json asset
7		JSON.parse(Assets.getText('repos.json')).forEach(function(it){
8			Repos.insert(it);
9		});
10	});
11}
12
13if (Meteor.isClient){
14  Template.demo.helpers({
15    repos: function() {
16      return Repos.find().fetch();
17    }
18  });
19}
1<input class="form-control typeahead" name="search" type="text" placeholder="Type to query"
2       autocomplete="off" spellcheck="off"
3       data-source="search"/>
1BigCollection = new Meteor.Collection('bigcollection');
2
3if (Meteor.isServer) {
4	Meteor.startup(function() {
5		if (!BigCollection.find().count()) {
6			// fill BigCollection
7		}
8	});
9
10	Meteor.methods({
11		search: function(query, options) {
12			options = options || {};
13
14			// guard against client-side DOS: hard limit to 50
15			if (options.limit) {
16				options.limit = Math.min(50, Math.abs(options.limit));
17			} else {
18				options.limit = 50;
19			}
20
21			// TODO fix regexp to support multiple tokens
22			var regex = new RegExp("^" + query);
23			return BigCollection.find({name: {$regex:  regex}}, options).fetch();
24		}
25	});
26} else {
27
28  Template.demo.helpers({
29    search = function(query, callback) {
30      Meteor.call('search', query, {}, function(err, res) {
31        if (err) {
32          console.log(err);
33          return;
34        }
35        callback(res.map(function(v){ return {value: v.name}; }));
36      });
37    }
38  });
39}

Initializing the typeahead

When the DOM is loaded through Meteor.startup on each template

1Meteor.startup(function() {
2  Meteor.typeahead.inject();
3});

with iron:router

Using iron:router the Meteor.startup is already triggered because it loads the template or the loading template and then inject the data. It must be delayed to when iron:router knows it is rendered completely.

1Template.demo.rendered = function() {
2  Meteor.typeahead.inject();
3};