Skip to content

Commit 0e65fee

Browse files
cdtinneyHaroenv
authored andcommitted
feat(source): add cache disabling for datasets (#254)
* Add cache disabling for datasets * Invert cache disable flag * Rename `enableCache` flag to `cache`
1 parent d343bee commit 0e65fee

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,7 @@ Datasets can be configured using the following options.
482482
* `debounce` – If set, will postpone the source execution until after `debounce` milliseconds
483483
have elapsed since the last time it was invoked.
484484

485+
* `cache` - If set to `false`, subsequent identical queries will always execute the source function for suggestions. Defaults to `true`.
485486

486487
## Sources
487488

src/autocomplete/dataset.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ function Dataset(o) {
3737

3838
this.debounce = o.debounce;
3939

40+
this.cache = o.cache !== false;
41+
4042
this.templates = getTemplates(o.templates, this.displayFn);
4143

4244
this.css = _.mixin({}, css, o.appendTo ? css.appendTo : {});
@@ -235,7 +237,10 @@ _.mixin(Dataset.prototype, EventEmitter, {
235237
},
236238

237239
shouldFetchFromCache: function shouldFetchFromCache(query) {
238-
return this.cachedQuery === query && this.cachedSuggestions && this.cachedSuggestions.length;
240+
return this.cache &&
241+
this.cachedQuery === query &&
242+
this.cachedSuggestions &&
243+
this.cachedSuggestions.length;
239244
},
240245

241246
clearCachedSuggestions: function clearCachedSuggestions() {

test/unit/dataset_spec.js

+24-1
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ describe('Dataset', function() {
291291
expect(this.dataset.cachedRenderExtraArgs).toEqual([42, true, false]);
292292
});
293293

294-
it('should retrieved cached results for subsequent identical queries', function() {
294+
it('should retrieve cached results for subsequent identical queries', function() {
295295
this.source.and.callFake(fakeGetWithSyncResults);
296296

297297
this.dataset.update('woah');
@@ -308,6 +308,29 @@ describe('Dataset', function() {
308308
expect(this.dataset.getRoot()).toContainText('three');
309309
});
310310

311+
it('should not retrieve cached results for subsequent identical queries if cache is disabled', function() {
312+
this.dataset = new Dataset({
313+
name: 'test',
314+
source: this.source = jasmine.createSpy('source'),
315+
cache: false,
316+
});
317+
318+
this.source.and.callFake(fakeGetWithSyncResultsAndExtraParams);
319+
320+
this.dataset.update('woah');
321+
expect(this.source.calls.count()).toBe(1);
322+
expect(this.dataset.getRoot()).toContainText('one');
323+
expect(this.dataset.getRoot()).toContainText('two');
324+
expect(this.dataset.getRoot()).toContainText('three');
325+
326+
this.dataset.clear();
327+
this.dataset.update('woah');
328+
expect(this.source.calls.count()).toBe(2);
329+
expect(this.dataset.getRoot()).toContainText('one');
330+
expect(this.dataset.getRoot()).toContainText('two');
331+
expect(this.dataset.getRoot()).toContainText('three');
332+
});
333+
311334
it('should reuse render function extra params for subsequent identical queries', function() {
312335
var spy = spyOn(this.dataset, '_render');
313336
this.source.and.callFake(fakeGetWithSyncResultsAndExtraParams);

0 commit comments

Comments
 (0)