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

Reduces dropdown flicker during a remote search. Fixes #176. #718

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/bloodhound/bloodhound.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ var Bloodhound = window.Bloodhound = (function() {
},

get: function get(query, cb) {
var that = this, matches, cacheHit = false;
var that = this, matches = [], cacheHit = false;

matches = this.index.get(query);
matches = this.sorter(matches).slice(0, this.limit);
Expand All @@ -164,8 +164,10 @@ var Bloodhound = window.Bloodhound = (function() {

// if a cache hit occurred, skip rendering local matches
// because the rendering of local/remote matches is already
// in the event loop
!cacheHit && cb && cb(matches);
// in the event loop.
// if we don't have any local matches and we're going to the
// network, don't render unnecessarily.
!cacheHit && (matches.length > 0 || !this.transport) && cb && cb(matches);

function returnRemoteMatches(remoteMatches) {
var matchesWithBackfill = matches.slice(0);
Expand All @@ -184,7 +186,6 @@ var Bloodhound = window.Bloodhound = (function() {
// the remote results and can break out of the each loop
return matchesWithBackfill.length < that.limit;
});

cb && cb(that.sorter(matchesWithBackfill));
}
},
Expand Down
4 changes: 3 additions & 1 deletion src/typeahead/typeahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,9 @@ var Typeahead = (function() {

_onQueryChanged: function onQueryChanged(e, query) {
this.input.clearHint();
this.dropdown.empty();
if(query.length === 0) {
this.dropdown.empty();
}
query.length >= this.minLength && this.dropdown.update(query);
this.dropdown.open();
this._setLanguageDirection();
Expand Down
90 changes: 76 additions & 14 deletions test/bloodhound_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,25 +283,87 @@ describe('Bloodhound', function() {
}
});

it('should call #get callback once if cache hit', function() {
var spy = jasmine.createSpy();
describe('when there is not matching data in the search index', function() {
beforeEach(function() {
this.bloodhound = new Bloodhound({
datumTokenizer: datumTokenizer,
queryTokenizer: queryTokenizer,
remote: '/test?q=%QUERY',
local: { value: 'not an animal' }
});

this.bloodhound = new Bloodhound({
datumTokenizer: datumTokenizer,
queryTokenizer: queryTokenizer,
remote: '/test?q=%QUERY'
this.bloodhound.initialize();
});
this.bloodhound.initialize();
this.bloodhound.transport.get.andCallFake(fakeGet);

this.bloodhound.get('dog', spy);
it('should call #get callback once if there is a cache hit', function() {
var spy = jasmine.createSpy();

expect(spy.callCount).toBe(1);
this.bloodhound.transport.get.andCallFake(fakeGetWithCacheHit);
this.bloodhound.get('dog', spy);

function fakeGet(url, o, cb) {
cb(fixtures.data.animals);
return true;
}
expect(spy.callCount).toBe(1);

function fakeGetWithCacheHit(url, o, cb) {
cb(fixtures.data.animals);
return true;
}
});

it('should call #get callback once if there is a cache miss', function() {
var spy = jasmine.createSpy();

this.bloodhound.transport.get.andCallFake(fakeGetWithCacheMiss);
this.bloodhound.get('dog', spy);

expect(spy.callCount).toBe(1);

function fakeGetWithCacheMiss(url, o, cb) {
cb(fixtures.data.animals);
return false;
}
});

});

describe('when there is matching data in the search index', function() {
beforeEach(function() {
this.bloodhound = new Bloodhound({
datumTokenizer: datumTokenizer,
queryTokenizer: queryTokenizer,
remote: '/test?q=%QUERY',
local: { value: 'dog' }
});

this.bloodhound.initialize();
});

it('should call the #get callback twice if there is a cache miss', function() {
var spy = jasmine.createSpy();

this.bloodhound.transport.get.andCallFake(fakeGetWithCacheMiss);
this.bloodhound.get('dog', spy);

expect(spy.callCount).toBe(2);

function fakeGetWithCacheMiss(url, o, cb) {
cb(fixtures.data.animals);
return false;
}
});

it('should call the #get callback once if there is a cache hit', function() {
var spy = jasmine.createSpy();

this.bloodhound.transport.get.andCallFake(fakeGetWithCacheHit);
this.bloodhound.get('dog', spy);

expect(spy.callCount).toBe(1);

function fakeGetWithCacheHit(url, o, cb) {
cb(fixtures.data.animals);
return true;
}
});
});
});

Expand Down
10 changes: 8 additions & 2 deletions test/typeahead_view_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,12 +376,18 @@ describe('Typeahead', function() {
expect(this.input.clearHint).toHaveBeenCalled();
});

it('should empty dropdown', function() {
this.input.trigger('queryChanged', testDatum.value);
it('should empty dropdown if the query is empty', function() {
this.input.trigger('queryChanged', '');

expect(this.dropdown.empty).toHaveBeenCalled();
});

it('should empty dropdown if the query is non-empty', function() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should not empty dropdown menu, right?

this.input.trigger('queryChanged', testDatum.value);

expect(this.dropdown.empty).not.toHaveBeenCalled();
});

it('should update dropdown', function() {
this.input.trigger('queryChanged', testDatum.value);

Expand Down