inst/htmlwidgets/lib/typeahead.js/doc/bloodhound.md

Bloodhound

Bloodhound is the typeahead.js suggestion engine. Bloodhound is robust, flexible, and offers advanced functionalities such as prefetching, intelligent caching, fast lookups, and backfilling with remote data.

Table of Contents

Features

Usage

API

new Bloodhound(options)

The constructor function. It takes an options hash as its only argument.

var engine = new Bloodhound({
  local: ['dog', 'pig', 'moose'],
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  datumTokenizer: Bloodhound.tokenizers.whitespace
});

Bloodhound.noConflict()

Returns a reference to Bloodhound and reverts window.Bloodhound to its previous value. Can be used to avoid naming collisions.

var Dachshund = Bloodhound.noConflict();

Bloodhound#initialize(reinitialize)

Kicks off the initialization of the suggestion engine. Initialization entails adding the data provided by local and prefetch to the internal search index as well as setting up transport mechanism used by remote. Before #initialize is called, the #get and #search methods will effectively be no-ops.

Note, unless the initialize option is false, this method is implicitly called by the constructor.

var engine = new Bloodhound({
  initialize: false,
  local: ['dog', 'pig', 'moose'],
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  datumTokenizer: Bloodhound.tokenizers.whitespace
});

var promise = engine.initialize();

promise
.done(function() { console.log('ready to go!'); })
.fail(function() { console.log('err, something went wrong :('); });

After initialization, how subsequent invocations of #initialize behave depends on the reinitialize argument. If reinitialize is falsy, the method will not execute the initialization logic and will just return the same jQuery promise returned by the initial invocation. If reinitialize is truthy, the method will behave as if it were being called for the first time.

var promise1 = engine.initialize();
var promise2 = engine.initialize();
var promise3 = engine.initialize(true);

assert(promise1 === promise2);
assert(promise3 !== promise1 && promise3 !== promise2);

Bloodhound#add(data)

Takes one argument, data, which is expected to be an array. The data passed in will get added to the internal search index.

engine.add([{ val: 'one' }, { val: 'two' }]);

Bloodhound#get(ids)

Returns the data in the local search index corresponding to ids.

  var engine = new Bloodhound({
    local: [{ id: 1, name: 'dog' }, { id: 2, name: 'pig' }],
    identify: function(obj) { return obj.id; },
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    datumTokenizer: Bloodhound.tokenizers.whitespace
  });

  engine.get([1, 3]); // [{ id: 1, name: 'dog' }, null]

Bloodhound#search(query, sync, async)

Returns the data that matches query. Matches found in the local search index will be passed to the sync callback. If the data passed to sync doesn't contain at least sufficient number of datums, remote data will be requested and then passed to the async callback.

bloodhound.get(myQuery, sync, async);

function sync(datums) {
  console.log('datums from `local`, `prefetch`, and `#add`');
  console.log(datums);
}

function async(datums) {
  console.log('datums from `remote`');
  console.log(datums);
}

Bloodhound#clear()

Clears the internal search index that's powered by local, prefetch, and #add.

engine.clear();

Options

When instantiating a Bloodhound suggestion engine, there are a number of options you can configure.

Prefetch

Prefetched data is fetched and processed on initialization. If the browser supports local storage, the processed data will be cached there to prevent additional network requests on subsequent page loads.

WARNING: While it's possible to get away with it for smaller data sets, prefetched data isn't meant to contain entire sets of data. Rather, it should act as a first-level cache. Ignoring this warning means you'll run the risk of hitting local storage limits.

When configuring prefetch, the following options are available.

Remote

Bloodhound only goes to the network when the internal search engine cannot provide a sufficient number of results. In order to prevent an obscene number of requests being made to the remote endpoint, requests are rate-limited.

When configuring remote, the following options are available.



edwindj/d3rug documentation built on May 15, 2019, 11:04 p.m.