From 6515c8ff199df5ccde65190b19188c2e1dbf6eb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20G=C3=BCnther?= Date: Tue, 11 Apr 2017 09:50:42 +0200 Subject: [PATCH] [24456] Debounce remote-field-updater rather than throttle --- .../common/remote/remote-field-updater.directive.test.ts | 5 ++++- .../common/remote/remote-field-updater.directive.ts | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/frontend/app/components/common/remote/remote-field-updater.directive.test.ts b/frontend/app/components/common/remote/remote-field-updater.directive.test.ts index 96327a40f3..ed4bfe5468 100644 --- a/frontend/app/components/common/remote/remote-field-updater.directive.test.ts +++ b/frontend/app/components/common/remote/remote-field-updater.directive.test.ts @@ -56,18 +56,21 @@ describe('remote-field-updater directive', function() { element.remove(); }); - it('should request the given url on input', function() { + it('should request the given url on input', function(done) { $httpBackend.expectGET('/foo/bar?q=foobar').respond(200, 'response!'); var input = element.find('.remote-field--input'); var e = jQuery.Event('keyup'); e.keyCode = 65; input.val('foobar').trigger(e); + setTimeout(() => { $httpBackend.flush(); $httpBackend.verifyNoOutstandingExpectation(); $httpBackend.verifyNoOutstandingRequest(); expect(element.find('.remote-field--target span').length).to.eql(1); + done(); + }, 1500); }); }); diff --git a/frontend/app/components/common/remote/remote-field-updater.directive.ts b/frontend/app/components/common/remote/remote-field-updater.directive.ts index 0fac0a08dc..0faaa1706c 100644 --- a/frontend/app/components/common/remote/remote-field-updater.directive.ts +++ b/frontend/app/components/common/remote/remote-field-updater.directive.ts @@ -86,7 +86,7 @@ function remoteFieldUpdater($http:ng.IHttpService) { }); } - input.on('keyup change', _.throttle(function(event:any) { + input.on('keyup change', _.debounce(function(event:any) { // This prevents an update of the result list when // tabbing to the result list (9), // pressing enter (13) @@ -97,7 +97,7 @@ function remoteFieldUpdater($http:ng.IHttpService) { if (keyCodesArray.indexOf(event.keyCode) == -1 && event.keyCode != undefined) { updater(); } - }, 1000, { leading: true }) + }, 1000) ); } };