From 0bc5ec4bb4c85358f3df4b393cb0c6f9af407dc5 Mon Sep 17 00:00:00 2001 From: Steffen van Bergerem Date: Wed, 10 Aug 2016 00:31:39 +0200 Subject: [PATCH] Make customsearch filter async results for typeahead.js --- app/assets/javascripts/app/views/search_base_view.js | 11 +++++------ app/assets/javascripts/main.js | 2 +- spec/javascripts/app/views/search_base_view_spec.js | 12 ++++++++---- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/app/assets/javascripts/app/views/search_base_view.js b/app/assets/javascripts/app/views/search_base_view.js index b96a98af8..72b032337 100644 --- a/app/assets/javascripts/app/views/search_base_view.js +++ b/app/assets/javascripts/app/views/search_base_view.js @@ -43,14 +43,14 @@ app.views.SearchBase = app.views.Base.extend({ setupCustomSearch: function() { var self = this; this.bloodhound.customSearch = function(query, sync, async) { - var _sync = function(datums) { + var _async = function(datums) { var results = datums.filter(function(datum) { 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, highlight: true, minLength: 2 - }, - { - name: "search", + }, { + async: true, display: "name", limit: 5, source: this.bloodhound.customSearch !== undefined ? this.bloodhound.customSearch : this.bloodhound, diff --git a/app/assets/javascripts/main.js b/app/assets/javascripts/main.js index ba2bb5e3d..c0a1af428 100644 --- a/app/assets/javascripts/main.js +++ b/app/assets/javascripts/main.js @@ -30,7 +30,7 @@ //= require markdown-it-sup //= require highlightjs //= require clear-form -//= require typeahead.js +//= require typeahead.bundle.js //= require app/app //= require diaspora //= require_tree ./helpers diff --git a/spec/javascripts/app/views/search_base_view_spec.js b/spec/javascripts/app/views/search_base_view_spec.js index 978d0d780..5ed6be4db 100644 --- a/spec/javascripts/app/views/search_base_view_spec.js +++ b/spec/javascripts/app/views/search_base_view_spec.js @@ -109,22 +109,26 @@ describe("app.views.SearchBase", function() { this.view = new app.views.SearchBase({ el: "#search_people_form", 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() { var spy = jasmine.createSpyObj("callbacks", ["syncCallback", "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() { var spy = jasmine.createSpyObj("callbacks", ["syncCallback", "asyncCallback"]); this.view.ignorePersonForSuggestions({handle: "user1@pod.tld"}); this.view.bloodhound.customSearch("user", spy.syncCallback, spy.asyncCallback); - expect(spy.syncCallback).toHaveBeenCalledWith([this.bloodhoundData[1]]); + expect(spy.asyncCallback).toHaveBeenCalledWith([this.bloodhoundData[1]]); }); }); });