npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

backbone.typeahead.js

v0.3.0

Published

A Bootstrap inspired Typeahead for Backbone.js

Downloads

479

Readme

Backbone.Typeahead

A Bootstrap inspired Typeahead for Backbone.js

Install

The typeahead can be installed with Bower:

bower install backbone.typeahead

or NPM:

npm install backbone.typeahead.js

This project targets Backbone 1.1.0 and above using Bootstrap 3. If you need 1.0.0 support, check out the v0.1.0 release of the typeahead. For an example using the old Bootstrap styles, see docs/bootstrap2.html.

Quickstart

To quickly create a typeahead, provide the the constructor with an array of objects to be searched. By default, the typeahead will search the key name.

var queryset = [
    {name: 'Super Bass-O-Matic 1976'},
    {name: 'Jam Hawkers'},
    {name: 'HiberNol'},
    {name: 'Colon Blow'},
];

var typeahead = new Backbone.Typeahead(queryset);
$('#main').html(typeahead.render().el);

The typeahead also plays nice with setElement:

typeahead.setElement('#main').render();

The typeahead will emit a selected event when the user selects an item. The selected model is accessible as the first parameter of the callback:

typeahead.on('selected', function(model) {
    console.log('The user has selected:', model);
});

Passing a Collection

The typeahead is built upon a Backbone.View and accepts a Backbone.Collection as the option collection:

var queryset = [
    {name: 'Super Bass-O-Matic 1976'},
    {name: 'Jam Hawkers'},
    {name: 'HiberNol'},
    {name: 'Colon Blow'},
];
var products = new Backbone.Collection(queryset);

var typeahead = new Backbone.Typeahead({collection: products});
typeahead.setElement('#main').render();

Options

Pass the option key to search by a different attribute:

var queryset = [
    {label: 'Super Bass-O-Matic 1976', year: 1976},
    {label: 'Jam Hawkers', year: 1977},
    {label: 'HiberNol', year: 1988},
    {label: 'Colon Blow', year: 1983},
];

var typeahead = new Backbone.Typeahead(queryset, {key: 'label'});
$('#main').html(typeahead.render().el);

Pass the option itemTemplate to render the search results with a different template:


var itemTemplate = '<a><strong><%- label %></strong> (<%- year %>)</a>';
var typeahead = new Backbone.Typeahead(queryset, {key: 'label', itemTemplate: itemTemplate});
$('#main').html(typeahead.render().el);

Using a Different Template

To use a different template for the typeahead itself, I recommend extending the Typeahead object:

var Extended = Backbone.Typeahead.extend({
  template: '<input type="text" class="form-control" placeholder="Find a State!"><ul class="dropdown-menu"></ul>',
});

If the new template has variables, the render method must also be extended:

var Extended = Backbone.Typeahead.extend({
  template: _.template('<input type="text" class="form-control" placeholder="<%- placeholder %>" /><ul class="dropdown-menu"></ul>'),
  render: function() {
    this.$el.html(this.template({
      placeholder: 'Hello, I am a placeholder!'
    }));

    // Don't forget these!
    this.$menu = this.$('ul');
    this.$input = this.$('input');
    return this;
  }
});

Extension

The typeahead was built for easy extension. Check out docs and spec for examples.

Minification

A minified version and source map are provided. Minification was performed with UglifyJS2 using the command:

uglifyjs backbone.typeahead.js -o backbone.typeahead.min.js --source-map backbone.typeahead.min.js.map -c -m

aodin, 2013-2015