• Stars
    star
    148
  • Rank 249,983 (Top 5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 11 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Autocomplete package for meteor powered by twitter typeahead.js

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.

Initializing typeahead

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

Meteor.startup(function() {
  Meteor.typeahead.inject();
});

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.

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

Examples

See demo application in this repository to find more examples.

data-source attribute

<input class="form-control typeahead" name="team" type="text"
       placeholder="NBA teams"
       autocomplete="off" spellcheck="off"
       data-source="nba"/>
Nba = new Meteor.Collection("nba");

if (Meteor.isServer){
	Nba.insert({name:'Boston Celtics'});
	// fill Nba collection
}

Template.demo.helpers({
  nba: function() {
    return Nba.find().fetch().map(function(it){ return it.name; });
  }
});

Multiple datasets

<template name="demo">
  <div class="form-group">
    <input class="form-control typeahead" name="team" type="text"
           placeholder="NBA and NHL teams"
           autocomplete="off" spellcheck="off"
           data-sets="teams"/>
  </div>
</template>

<template name="team">
	<h4><i>{{name}}</i></h4>
</template>
Template.demo.helpers({
  teams: function() {
    return [
      {
        name: 'nba-teams',
        valueKey: 'name',
        local: function() { return Nba.find().fetch(); },
        header: '<h3 class="league-name">NBA Teams</h3>',
        template: 'team'
      },
      {
        name: 'nhl-teams',
        valueKey: 'name',
        local: function() { return Nhl.find().fetch(); },
        header: '<h3 class="league-name">NHL Teams</h3>',
        template: 'team'
      }
    ];
  }
});

Custom template to render suggestion

<input class="form-control typeahead" name="repo" type="text"
       placeholder="open source projects by Twitter"
       autocomplete="off" spellcheck="off"
       data-source="repos" data-template="repo"/>

<template name="repo">
       <p class="repo-language">{{language}}</p>
       <p class="repo-name">{{name}}</p>
       <p class="repo-description">{{description}}</p>
</template>
Repos = new Meteor.Collection("repos");

if (Meteor.isServer){
	Meteor.startup(function(){
		Repos.remove({});
		// fill repos from private repos.json asset
		JSON.parse(Assets.getText('repos.json')).forEach(function(it){
			Repos.insert(it);
		});
	});
}

if (Meteor.isClient){
  Template.demo.helpers({
    repos: function() {
      // this only works if returned objects have
      // an attribute named "value" containing the text
      // See docs for "data-value-key" attribute
      return Repos.find().fetch();
    }
  });
}

Server side search

<input class="form-control typeahead" name="search" type="text" placeholder="Type to query"
       autocomplete="off" spellcheck="off"
       data-source="search"/>
BigCollection = new Meteor.Collection('bigcollection');

if (Meteor.isServer) {
	Meteor.startup(function() {
		if (!BigCollection.find().count()) {
			// fill BigCollection
		}
	});

	Meteor.methods({
		search: function(query, options) {
			options = options || {};

			// guard against client-side DOS: hard limit to 50
			if (options.limit) {
				options.limit = Math.min(50, Math.abs(options.limit));
			} else {
				options.limit = 50;
			}

			// TODO fix regexp to support multiple tokens
			var regex = new RegExp("^" + query);
			return BigCollection.find({name: {$regex:  regex}}, options).fetch();
		}
	});
} else {

  Template.demo.helpers({
    search = function(query, sync, callback) {
      Meteor.call('search', query, {}, function(err, res) {
        if (err) {
          console.log(err);
          return;
        }
        callback(res.map(function(v){ return {value: v.name}; }));
      });
    }
  });
}

Catching selected event with id

Template.example.rendered = function() {
  Meteor.typeahead.inject();
}

Template.example.helpers({
  items: function() {
    // data source function
    // TODO fetch items from meteor collection
    return someCollections.find().fetch().map(function(object){ return {id: object._id, value: object.value}; });
  },
  selected: function(event, suggestion, datasetName) {
    // event - the jQuery event object
    // suggestion - the suggestion object
    // datasetName - the name of the dataset the suggestion belongs to
    // TODO your event handler here
    console.log(suggestion.id);
  }
});

Template:

<template name="example">
  <input placeholder="Kies een plaats" autocomplete="off" spellcheck="off"
      data-source="items" data-select="selected"/>
</template>

Styling

By default, there is no style applied with this package. If you want the same styling as in the demo app, please do the following:

  • add bootstrap: meteor add twbs:bootstrap
  • add the style.css file to your application

More Repositories

1

parse-diff

Unified diff parser for nodejs and browser
JavaScript
84
star
2

fint

.NET CIL interpreter written in simple subset of F#
F#
51
star
3

karma-typescript-preprocessor

TypeScript preprocessor for karma-runner
TypeScript
50
star
4

pandora

Small box of pandora to prototype your app with ready for use backend. This is just my compilation of different solutions occasionally applied in hackathons and challenges
Go
26
star
5

Owin.Routing

Simple routing for OWIN applications inspired by express.js
C#
12
star
6

CronDaemon

.NET library with single CronDaemon class with generic implementation of cron scheduling based on ncrontab
C#
11
star
7

meteor-dimple

Meteor package for dimple.js library
JavaScript
10
star
8

d3-gems

Collection of reusable widgets powered by d3.js
JavaScript
6
star
9

knockout-handlebars

Handlebars syntax sugar for knockout.js
JavaScript
6
star
10

jquery-bootswatch

Bootswatch themes switcher
JavaScript
5
star
11

meteor-gist

Handlebars helper to inject gist into dynamic meteor template
JavaScript
5
star
12

kanban

Realtime kanban boards powered by meteor
CoffeeScript
4
star
13

cubeslam

Automatically exported from code.google.com/p/cubeslam
JavaScript
4
star
14

scraper

Declarative web scraper in JavaScript primarily designed to extract linguistics data
TypeScript
4
star
15

meteor-d3

DEPRECATED: meteor package for d3js library.
CoffeeScript
4
star
16

gulp-duo

Gulp plugin to run duo package manager
JavaScript
3
star
17

SharpExpress

Functional API to process HTTP requests with ASP.NET MVC-like routing inspired by express.js
C#
2
star
18

korest

Function for KnockoutJS to map plain object into object with knockout observable properties bound to REST actions
CoffeeScript
2
star
19

fetch-stream

Easy fetch of HTTP/1.1 chunked content.
JavaScript
2
star
20

kanban-vision

Integrated kanban boards
JavaScript
1
star
21

teamcity.js

TeamCity client runnable in nodejs and browser environments
JavaScript
1
star
22

rxjs-operators-bundle

bundle with all rxjs operators
JavaScript
1
star
23

more

Terminal pager of long output for nodejs
JavaScript
1
star
24

multidisk

Mini Google Drive clone :) powered by Uploadcare
TypeScript
1
star
25

NJade

Jade view engine for .NET
C#
1
star
26

xserializer

Simple schema-oriented serialization engine for .NET.
C#
1
star
27

hg.js

Simple api to run mercurial commands from nodejs
CoffeeScript
1
star
28

restorm

Package to build RESTful API for gorm models
Go
1
star
29

iswin

Nodejs function to detect windows platform
JavaScript
1
star
30

miki

Simple markdown wiki pages server
CSS
1
star
31

biolink-scraper

extract links from link aggregators like https://bio.link
JavaScript
1
star
32

cil.js

.NET CIL reader in JavaScript
JavaScript
1
star
33

nunbars

Handlerbars-like templating engine that builds virtual DOMs
CoffeeScript
1
star