twitter/typeahead.js — pull request #220 by brianmhunt (2013-04-28 13:54:19+00:00)
Title: Added 'compute' value to the Dataset, along with corresponding unit tests

Regarding Issue #95, this pull implements a way to address the issue of sync/async arbitrary computed datasets. While it may not be what you had in mind or the direction you want to take, I hope it gives helpful food for thought.

If this were merged the datasets arguments would accept a new `computed` argument. In the synchronous case that computed option takes a single argument, the query.

```javascript
this.dataset = new Dataset({
computed: function (q) {
return ['charlie'];
}
});
```

An asynchronous version of `computed` takes two arguments - the query and a `done` callback that the user calls with the results they wish to add to the suggestions.

```javascript
this.dataset = new Dataset({
computed: function (q, done) {
setTimeout(function () { done(['alpha', 'beta', 'lucy']); }, 0);
}
});
```

Note that the asynchronous version uses the same logic as `remote` requests to check for duplicate results, but the synchronous version is like `local` in that it does not do any checks.

Neither synchronous nor async utilizes the typeahead caching. It is left to the user to determine if and how they want to cache results, as I expect more complex applications would want to.

I hope that proves useful and interesting. Cheers.