Skip to content

Commit a47696d

Browse files
committed
fix(dataset): avoid to call the source when upadte is canceled
1 parent 1a0ce74 commit a47696d

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/autocomplete/dataset.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,13 @@ _.mixin(Dataset.prototype, EventEmitter, {
207207
handleSuggestions.apply(this, [this.cachedSuggestions].concat(this.cachedRenderExtraArgs));
208208
} else {
209209
var that = this;
210-
var execSource = function() { that.source(query, handleSuggestions.bind(that)); };
210+
var execSource = function() {
211+
// When the call is debounced the condition avoid to do a useless
212+
// request with the last character when the input has been cleared
213+
if (!that.canceled) {
214+
that.source(query, handleSuggestions.bind(that));
215+
}
216+
};
211217

212218
if (this.debounce) {
213219
var later = function() {

test/unit/dataset_spec.js

+25
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,31 @@ describe('Dataset', function() {
364364
done();
365365
}, 500);
366366
});
367+
368+
it('should not call the source if update was canceled', function(done) {
369+
var that = this;
370+
371+
this.dataset = new Dataset({
372+
source: this.source,
373+
debounce: 250
374+
});
375+
376+
this.source.and.callFake(fakeGetWithSyncResultsAndExtraParams);
377+
378+
this.dataset.update('woah');
379+
expect(this.source.calls.count()).toBe(0);
380+
381+
this.dataset.update('woah 2');
382+
expect(this.source.calls.count()).toBe(0);
383+
384+
this.dataset.clear();
385+
expect(this.source.calls.count()).toBe(0);
386+
387+
setTimeout(function() {
388+
expect(that.source.calls.count()).toBe(0);
389+
done();
390+
}, 500);
391+
});
367392
});
368393

369394
describe('#cacheSuggestions', function() {

0 commit comments

Comments
 (0)