Skip to content

Commit 4fe2a34

Browse files
author
Jake Harding
committed
Merge remote-tracking branch 'origin/1148-index-remote' into integration-0.11.2
2 parents 212fe89 + f885f48 commit 4fe2a34

File tree

5 files changed

+73
-10
lines changed

5 files changed

+73
-10
lines changed

doc/bloodhound.md

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@ options you can configure.
193193
the internal search index is insufficient or, if more configurability is
194194
needed, a [remote options hash](#remote).
195195

196+
* `indexRemote` – Adds the data loaded from `remote` to the search index (where
197+
`local` and `prefetch` are stored for retrieval). Defaults to `false`.
198+
196199
<!-- section links -->
197200

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

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

261264
* `wildcard` – A convenience option for `prepare`. If set, `prepare` will be a
262-
function that replaces the value of this option in `url` with the URI encoded
263-
query.
265+
function that replaces the value of this option in `url` with the URI encoded
266+
query.
264267

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

271274
* `transform` – A function with the signature `transform(response)` that allows
272-
you to transform the remote response before the Bloodhound instance operates
273-
on it. Defaults to the [identity function].
275+
you to transform the remote response before the Bloodhound instance operates
276+
on it. Defaults to the [identity function].
274277

275278
<!-- section links -->
276279

src/bloodhound/bloodhound.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ var Bloodhound = (function() {
2020
this.sorter = o.sorter;
2121
this.identify = o.identify;
2222
this.sufficient = o.sufficient;
23+
this.indexRemote = o.indexRemote;
2324

2425
this.local = o.local;
2526
this.remote = o.remote ? new Remote(o.remote) : null;
@@ -132,6 +133,9 @@ var Bloodhound = (function() {
132133
search: function search(query, sync, async) {
133134
var that = this, local;
134135

136+
sync = sync || _.noop;
137+
async = async || _.noop;
138+
135139
local = this.sorter(this.index.search(query));
136140

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

162-
async && async(nonDuplicates);
166+
// #1148: Should Bloodhound index remote datums?
167+
that.indexRemote && that.add(nonDuplicates);
168+
169+
async(nonDuplicates);
163170
}
164171
},
165172

src/bloodhound/options_parser.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var oParser = (function() {
1616
datumTokenizer: null,
1717
queryTokenizer: null,
1818
sufficient: 5,
19+
indexRemote: false,
1920
sorter: null,
2021
local: [],
2122
prefetch: null,

src/bloodhound/remote.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ var Remote = (function() {
1414
this.url = o.url;
1515
this.prepare = o.prepare;
1616
this.transform = o.transform;
17+
this.indexResponse = o.indexResponse;
1718

1819
this.transport = new Transport({
1920
cache: o.cache,

test/bloodhound/bloodhound_spec.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,57 @@ describe('Bloodhound', function() {
289289

290290
function fakeGet(o, cb) { cb(fixtures.data.animals); }
291291
});
292+
293+
it('should not add remote data to index if indexRemote is false', function() {
294+
this.bloodhound = build({
295+
identify: function(d) { return d.value; },
296+
remote: '/remote'
297+
});
298+
this.bloodhound.remote.get.andCallFake(fakeGet);
299+
300+
spyOn(this.bloodhound, 'add');
301+
this.bloodhound.search('dog');
302+
303+
expect(this.bloodhound.add).not.toHaveBeenCalled();
304+
305+
function fakeGet(o, cb) { cb(fixtures.data.animals); }
306+
});
307+
308+
it('should add remote data to index if indexRemote is true', function() {
309+
this.bloodhound = build({
310+
identify: function(d) { return d.value; },
311+
indexRemote: true,
312+
remote: '/remote'
313+
});
314+
this.bloodhound.remote.get.andCallFake(fakeGet);
315+
316+
spyOn(this.bloodhound, 'add');
317+
this.bloodhound.search('dog');
318+
319+
expect(this.bloodhound.add).toHaveBeenCalledWith(fixtures.data.animals);
320+
321+
function fakeGet(o, cb) { cb(fixtures.data.animals); }
322+
});
323+
324+
it('should not add duplicates from remote to index', function() {
325+
this.bloodhound = build({
326+
identify: function(d) { return d.value; },
327+
indexRemote: true,
328+
local: fixtures.data.animals,
329+
remote: '/remote'
330+
});
331+
this.bloodhound.remote.get.andCallFake(fakeGet);
332+
333+
spyOn(this.bloodhound, 'add');
334+
this.bloodhound.search('dog');
335+
336+
expect(this.bloodhound.add).toHaveBeenCalledWith([
337+
{ value: 'cat' },
338+
{ value: 'moose' }
339+
]);
340+
341+
function fakeGet(o, cb) { cb(fixtures.data.animals); }
342+
});
292343
});
293344

294345
// helper functions

0 commit comments

Comments
 (0)