From 35a792b3ca43991e0fbbcfd57e9dddbaa79ff3bc Mon Sep 17 00:00:00 2001 From: Will Marshall Date: Fri, 18 Apr 2014 16:53:43 +1000 Subject: [PATCH] Debouncing things --- .../darkswarm/directives/debounce.js.coffee | 25 +++++++++++++++++++ app/views/darkswarm/index.html.haml | 4 ++- 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 app/assets/javascripts/darkswarm/directives/debounce.js.coffee diff --git a/app/assets/javascripts/darkswarm/directives/debounce.js.coffee b/app/assets/javascripts/darkswarm/directives/debounce.js.coffee new file mode 100644 index 0000000000..4ba950bf6a --- /dev/null +++ b/app/assets/javascripts/darkswarm/directives/debounce.js.coffee @@ -0,0 +1,25 @@ +Darkswarm.directive "ngDebounce", ($timeout) -> + restrict: "A" + require: "ngModel" + priority: 99 + link: (scope, elm, attr, ngModelCtrl) -> + return if attr.type is "radio" or attr.type is "checkbox" + elm.unbind "input" + debounce = undefined + elm.bind "keyup paste", -> + $timeout.cancel debounce + debounce = $timeout(-> + scope.$apply -> + ngModelCtrl.$setViewValue elm.val() + return + return + , attr.ngDebounce or 1000) + return + + elm.bind "blur", -> + scope.$apply -> + ngModelCtrl.$setViewValue elm.val() + return + return + return + diff --git a/app/views/darkswarm/index.html.haml b/app/views/darkswarm/index.html.haml index 6f7a454526..57a121b509 100644 --- a/app/views/darkswarm/index.html.haml +++ b/app/views/darkswarm/index.html.haml @@ -23,7 +23,9 @@ .row .large-9.columns - %input{type: :text, "ng-model" => "query", placeholder: "Search postcode, suburb or hub name...", + %input{type: :text, "ng-model" => "query", + placeholder: "Search postcode, suburb or hub name...", + "ng-debounce" => "150", "ofn-disable-enter" => true} .large-3.columns Advanced search