From 7ef01304ee326dd64e812eaec8b528bead6720c9 Mon Sep 17 00:00:00 2001 From: Francis Lachapelle Date: Wed, 1 Apr 2015 09:52:16 -0400 Subject: [PATCH] Add debounce to sgSearch Angular directive --- UI/WebServerResources/js/Common/ui-desktop.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/UI/WebServerResources/js/Common/ui-desktop.js b/UI/WebServerResources/js/Common/ui-desktop.js index 04be0a941..bc4578dc4 100644 --- a/UI/WebServerResources/js/Common/ui-desktop.js +++ b/UI/WebServerResources/js/Common/ui-desktop.js @@ -625,7 +625,7 @@ selectEl = tElement.find('md-select'); inputEl.attr('ng-model', '$sgSearchController.searchText'); - inputEl.attr('ng-keyup', '$sgSearchController.onChange()'); + inputEl.attr('ng-model-options', '$sgSearchController.searchTextOptions'); selectEl.attr('ng-model', '$sgSearchController.searchField'); selectEl.attr('ng-change', '$sgSearchController.onChange()'); @@ -633,14 +633,28 @@ $compile(mdInputEl)(scope); $compile(selectEl)(scope); $compile(tElement.find('md-button'))(scope.$parent); + + scope.$watch('$sgSearchController.searchText', angular.bind(controller, controller.onChange)); } } }]) .controller('sgSearchController', ['$scope', '$element', function($scope, $element) { + // Controller variables this.previous = { searchText: '', searchField: '' }; + this.searchText = ''; this.searchField = $element.find('md-option').attr('value'); // defaults to first option - this.onChange = function() { + // Model options + this.searchTextOptions = { + updateOn: 'default blur', + debounce: { + default: 300, + blur: 0 + } + }; + + // Method to call on data changes + this.onChange = function(value) { if (typeof this.searchText != 'undefined') { if (this.searchText != this.previous.searchText || this.searchField != this.previous.searchField) { if (this.searchText.length > 2 || this.searchText.length == 0) {