From 12b6d8a0320514bb740b0fbc8df04f0ecce36f8d Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 10 Aug 2015 16:12:33 +1000 Subject: [PATCH] FIX: cancel in progress search when flipping to full search --- .../discourse/controllers/search.js.es6 | 33 ++++++++++++++++--- .../discourse/lib/search-for-term.js.es6 | 6 +++- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/app/assets/javascripts/discourse/controllers/search.js.es6 b/app/assets/javascripts/discourse/controllers/search.js.es6 index 9c44f0b2a..0e1aa7a1f 100644 --- a/app/assets/javascripts/discourse/controllers/search.js.es6 +++ b/app/assets/javascripts/discourse/controllers/search.js.es6 @@ -85,20 +85,32 @@ export default Em.Controller.extend(Presence, { searchTerm: function(term, typeFilter) { var self = this; + // for cancelling debounced search + if (this._cancelSearch){ + this._cancelSearch = null; + return; + } + + if (this._search) { + this._search.abort(); + } + var context; if(this.get('searchContextEnabled')){ context = this.get('searchContext'); } - searchForTerm(term, { + this._search = searchForTerm(term, { typeFilter: typeFilter, searchContext: context, fullSearchUrl: this.get('fullSearchUrl') - }).then(function(results) { + }); + + this._search.then(function(results) { self.setProperties({ noResults: !results, content: results }); + }).finally(function() { self.set('loading', false); - }).catch(function() { - self.set('loading', false); + self._search = null; }); }, @@ -113,6 +125,19 @@ export default Em.Controller.extend(Presence, { actions: { fullSearch: function() { + const self = this; + + if (this._search) { + this._search.abort(); + } + + // maybe we are debounced and delayed + // stop that as well + this._cancelSearch = true; + Em.run.later(function(){ + self._cancelSearch = false; + }, 400); + var url = this.get('fullSearchUrlRelative'); if (url) { Discourse.URL.routeTo(url); diff --git a/app/assets/javascripts/discourse/lib/search-for-term.js.es6 b/app/assets/javascripts/discourse/lib/search-for-term.js.es6 index e5348a6a1..2d029fdf9 100644 --- a/app/assets/javascripts/discourse/lib/search-for-term.js.es6 +++ b/app/assets/javascripts/discourse/lib/search-for-term.js.es6 @@ -77,9 +77,13 @@ function searchForTerm(term, opts) { }; } - return Discourse.ajax('/search/query', { data: data }).then(function(results){ + var promise = Discourse.ajax('/search/query', { data: data }); + + promise.then(function(results){ return translateResults(results, opts); }); + + return promise; } export default searchForTerm;