Make customsearch filter async results for typeahead.js

This commit is contained in:
Steffen van Bergerem 2016-08-10 00:31:39 +02:00
parent 5865314172
commit 0bc5ec4bb4
No known key found for this signature in database
GPG key ID: 2F08F75F9525C7E0
3 changed files with 14 additions and 11 deletions

View file

@ -43,14 +43,14 @@ app.views.SearchBase = app.views.Base.extend({
setupCustomSearch: function() { setupCustomSearch: function() {
var self = this; var self = this;
this.bloodhound.customSearch = function(query, sync, async) { this.bloodhound.customSearch = function(query, sync, async) {
var _sync = function(datums) { var _async = function(datums) {
var results = datums.filter(function(datum) { var results = datums.filter(function(datum) {
return datum.handle !== undefined && self.ignoreDiasporaIds.indexOf(datum.handle) === -1; return datum.handle !== undefined && self.ignoreDiasporaIds.indexOf(datum.handle) === -1;
}); });
sync(results); async(results);
}; };
self.bloodhound.search(query, _sync, async); self.bloodhound.search(query, sync, _async);
}; };
}, },
@ -59,9 +59,8 @@ app.views.SearchBase = app.views.Base.extend({
hint: false, hint: false,
highlight: true, highlight: true,
minLength: 2 minLength: 2
}, }, {
{ async: true,
name: "search",
display: "name", display: "name",
limit: 5, limit: 5,
source: this.bloodhound.customSearch !== undefined ? this.bloodhound.customSearch : this.bloodhound, source: this.bloodhound.customSearch !== undefined ? this.bloodhound.customSearch : this.bloodhound,

View file

@ -30,7 +30,7 @@
//= require markdown-it-sup //= require markdown-it-sup
//= require highlightjs //= require highlightjs
//= require clear-form //= require clear-form
//= require typeahead.js //= require typeahead.bundle.js
//= require app/app //= require app/app
//= require diaspora //= require diaspora
//= require_tree ./helpers //= require_tree ./helpers

View file

@ -109,22 +109,26 @@ describe("app.views.SearchBase", function() {
this.view = new app.views.SearchBase({ this.view = new app.views.SearchBase({
el: "#search_people_form", el: "#search_people_form",
typeaheadInput: $("#q"), typeaheadInput: $("#q"),
customSearch: true customSearch: true,
remoteRoute: "/contacts"
}); });
this.view.bloodhound.add(this.bloodhoundData); this.view.bloodhound.search = function(query, sync, async) {
sync([]);
async(this.bloodhoundData);
}.bind(this);
}); });
it("returns all results if none of them should be ignored", function() { it("returns all results if none of them should be ignored", function() {
var spy = jasmine.createSpyObj("callbacks", ["syncCallback", "asyncCallback"]); var spy = jasmine.createSpyObj("callbacks", ["syncCallback", "asyncCallback"]);
this.view.bloodhound.customSearch("user", spy.syncCallback, spy.asyncCallback); this.view.bloodhound.customSearch("user", spy.syncCallback, spy.asyncCallback);
expect(spy.syncCallback).toHaveBeenCalledWith(this.bloodhoundData); expect(spy.asyncCallback).toHaveBeenCalledWith(this.bloodhoundData);
}); });
it("doesn't return results that should be ignored", function() { it("doesn't return results that should be ignored", function() {
var spy = jasmine.createSpyObj("callbacks", ["syncCallback", "asyncCallback"]); var spy = jasmine.createSpyObj("callbacks", ["syncCallback", "asyncCallback"]);
this.view.ignorePersonForSuggestions({handle: "user1@pod.tld"}); this.view.ignorePersonForSuggestions({handle: "user1@pod.tld"});
this.view.bloodhound.customSearch("user", spy.syncCallback, spy.asyncCallback); this.view.bloodhound.customSearch("user", spy.syncCallback, spy.asyncCallback);
expect(spy.syncCallback).toHaveBeenCalledWith([this.bloodhoundData[1]]); expect(spy.asyncCallback).toHaveBeenCalledWith([this.bloodhoundData[1]]);
}); });
}); });
}); });