Skip to content

Commit

Permalink
fix(dataset): avoid to call the source when upadte is canceled
Browse files Browse the repository at this point in the history
  • Loading branch information
samouss committed May 24, 2018
1 parent 1a0ce74 commit a47696d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/autocomplete/dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,13 @@ _.mixin(Dataset.prototype, EventEmitter, {
handleSuggestions.apply(this, [this.cachedSuggestions].concat(this.cachedRenderExtraArgs));
} else {
var that = this;
var execSource = function() { that.source(query, handleSuggestions.bind(that)); };
var execSource = function() {
// When the call is debounced the condition avoid to do a useless
// request with the last character when the input has been cleared
if (!that.canceled) {
that.source(query, handleSuggestions.bind(that));
}
};

if (this.debounce) {
var later = function() {
Expand Down
25 changes: 25 additions & 0 deletions test/unit/dataset_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,31 @@ describe('Dataset', function() {
done();
}, 500);
});

it('should not call the source if update was canceled', function(done) {
var that = this;

this.dataset = new Dataset({
source: this.source,
debounce: 250
});

this.source.and.callFake(fakeGetWithSyncResultsAndExtraParams);

this.dataset.update('woah');
expect(this.source.calls.count()).toBe(0);

this.dataset.update('woah 2');
expect(this.source.calls.count()).toBe(0);

this.dataset.clear();
expect(this.source.calls.count()).toBe(0);

setTimeout(function() {
expect(that.source.calls.count()).toBe(0);
done();
}, 500);
});
});

describe('#cacheSuggestions', function() {
Expand Down

0 comments on commit a47696d

Please sign in to comment.