Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v0.11.2 #1193

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
2 changes: 0 additions & 2 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ module.exports = function(grunt) {
bloodhound: {
src: '<%= tempDir %>/bloodhound.js',
objectToExport: 'Bloodhound',
amdModuleId: 'bloodhound',
deps: {
default: ['$'],
amd: ['jquery'],
Expand All @@ -145,7 +144,6 @@ module.exports = function(grunt) {
},
typeahead: {
src: '<%= tempDir %>/typeahead.jquery.js',
amdModuleId: 'typeahead.js',
deps: {
default: ['$'],
amd: ['jquery'],
Expand Down
21 changes: 12 additions & 9 deletions doc/bloodhound.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ options you can configure.
the internal search index is insufficient or, if more configurability is
needed, a [remote options hash](#remote).

* `indexRemote` – Adds the data loaded from `remote` to the search index (where
`local` and `prefetch` are stored for retrieval). Defaults to `false`.

<!-- section links -->

[compare function]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
Expand Down Expand Up @@ -252,15 +255,15 @@ When configuring `remote`, the following options are available.
* `url` – The URL remote data should be loaded from. **Required.**

* `prepare` – A function that provides a hook to allow you to prepare the
settings object passed to `transport` when a request is about to be made.
The function signature should be `prepare(query, settings)`, where `query` is
the query `#search` was called with and `settings` is the default settings
object created internally by the Bloodhound instance. The `prepare` function
should return a settings object. Defaults to the [identity function].
settings object passed to `transport` when a request is about to be made.
The function signature should be `prepare(query, settings)`, where `query` is
the query `#search` was called with and `settings` is the default settings
object created internally by the Bloodhound instance. The `prepare` function
should return a settings object. Defaults to the [identity function].

* `wildcard` – A convenience option for `prepare`. If set, `prepare` will be a
function that replaces the value of this option in `url` with the URI encoded
query.
function that replaces the value of this option in `url` with the URI encoded
query.

* `rateLimitBy` – The method used to rate-limit network requests. Can be either
`debounce` or `throttle`. Defaults to `debounce`.
Expand All @@ -269,8 +272,8 @@ When configuring `remote`, the following options are available.
`rateLimitBy`. Defaults to `300`.

* `transform` – A function with the signature `transform(response)` that allows
you to transform the remote response before the Bloodhound instance operates
on it. Defaults to the [identity function].
you to transform the remote response before the Bloodhound instance operates
on it. Defaults to the [identity function].

<!-- section links -->

Expand Down
9 changes: 8 additions & 1 deletion src/bloodhound/bloodhound.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var Bloodhound = (function() {
this.sorter = o.sorter;
this.identify = o.identify;
this.sufficient = o.sufficient;
this.indexRemote = o.indexRemote;

this.local = o.local;
this.remote = o.remote ? new Remote(o.remote) : null;
Expand Down Expand Up @@ -132,6 +133,9 @@ var Bloodhound = (function() {
search: function search(query, sync, async) {
var that = this, local;

sync = sync || _.noop;
async = async || _.noop;

local = this.sorter(this.index.search(query));

// return a copy to guarantee no changes within this scope
Expand Down Expand Up @@ -159,7 +163,10 @@ var Bloodhound = (function() {
}) && nonDuplicates.push(r);
});

async && async(nonDuplicates);
// #1148: Should Bloodhound index remote datums?
that.indexRemote && that.add(nonDuplicates);

async(nonDuplicates);
}
},

Expand Down
1 change: 1 addition & 0 deletions src/bloodhound/options_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var oParser = (function() {
datumTokenizer: null,
queryTokenizer: null,
sufficient: 5,
indexRemote: false,
sorter: null,
local: [],
prefetch: null,
Expand Down
1 change: 1 addition & 0 deletions src/bloodhound/remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var Remote = (function() {
this.url = o.url;
this.prepare = o.prepare;
this.transform = o.transform;
this.indexResponse = o.indexResponse;

this.transport = new Transport({
cache: o.cache,
Expand Down
1 change: 0 additions & 1 deletion src/typeahead/dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@ var Dataset = (function() {
// do not render the suggestions as they've become outdated
if (!canceled && rendered < that.limit) {
that.cancel = $.noop;
rendered += suggestions.length;
that._append(query, suggestions.slice(0, that.limit - rendered));

that.async && that.trigger('asyncReceived', query);
Expand Down
1 change: 1 addition & 0 deletions src/typeahead/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ var Menu = (function() {
},

open: function open() {
this.$node.scrollTop(0);
this.$node.addClass(this.classes.open);
},

Expand Down
2 changes: 1 addition & 1 deletion src/typeahead/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@
}

else {
ttEach(this, function(t) { t.setVal(newVal); });
ttEach(this, function(t) { t.setVal(_.toStr(newVal)); });
return this;
}
},
Expand Down
51 changes: 51 additions & 0 deletions test/bloodhound/bloodhound_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,57 @@ describe('Bloodhound', function() {

function fakeGet(o, cb) { cb(fixtures.data.animals); }
});

it('should not add remote data to index if indexRemote is false', function() {
this.bloodhound = build({
identify: function(d) { return d.value; },
remote: '/remote'
});
this.bloodhound.remote.get.andCallFake(fakeGet);

spyOn(this.bloodhound, 'add');
this.bloodhound.search('dog');

expect(this.bloodhound.add).not.toHaveBeenCalled();

function fakeGet(o, cb) { cb(fixtures.data.animals); }
});

it('should add remote data to index if indexRemote is true', function() {
this.bloodhound = build({
identify: function(d) { return d.value; },
indexRemote: true,
remote: '/remote'
});
this.bloodhound.remote.get.andCallFake(fakeGet);

spyOn(this.bloodhound, 'add');
this.bloodhound.search('dog');

expect(this.bloodhound.add).toHaveBeenCalledWith(fixtures.data.animals);

function fakeGet(o, cb) { cb(fixtures.data.animals); }
});

it('should not add duplicates from remote to index', function() {
this.bloodhound = build({
identify: function(d) { return d.value; },
indexRemote: true,
local: fixtures.data.animals,
remote: '/remote'
});
this.bloodhound.remote.get.andCallFake(fakeGet);

spyOn(this.bloodhound, 'add');
this.bloodhound.search('dog');

expect(this.bloodhound.add).toHaveBeenCalledWith([
{ value: 'cat' },
{ value: 'moose' }
]);

function fakeGet(o, cb) { cb(fixtures.data.animals); }
});
});

// helper functions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ describe('Menu', function() {
});

describe('#open', function() {
it('should set scroll top of node to 0', function() {
spyOn(this.view.$node, 'scrollTop');
this.view.open();

expect(this.view.$node.scrollTop).toHaveBeenCalledWith(0);
});

it('should add open class to node', function() {
this.$node.removeClass(www.classes.open);
this.view.open();
Expand Down
8 changes: 8 additions & 0 deletions test/typeahead/plugin_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,14 @@ describe('$plugin', function() {
expect(this.$input.typeahead('val')).toBe('foo');
});

it('#val(q) should coerce null and undefined to empty string', function() {
this.$input.typeahead('val', null);
expect(this.$input.typeahead('val')).toBe('');

this.$input.typeahead('val', undefined);
expect(this.$input.typeahead('val')).toBe('');
});

it('#destroy should revert modified attributes', function() {
expect(this.$input).toHaveAttr('autocomplete', 'off');
expect(this.$input).toHaveAttr('dir');
Expand Down