From 7f2aeead0af069d49cf250dc41b1faa30356d9e1 Mon Sep 17 00:00:00 2001 From: Jake Harding Date: Wed, 13 Mar 2013 23:35:49 -0700 Subject: [PATCH] Defer on triggering typeahead:initialized. --- src/dataset.js | 2 +- src/input_view.js | 2 +- src/typeahead.js | 6 +++++- src/typeahead_view.js | 2 +- src/utils.js | 2 ++ test/playground.html | 14 +++++++------- 6 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/dataset.js b/src/dataset.js index eda0b9c..1a074e1 100644 --- a/src/dataset.js +++ b/src/dataset.js @@ -156,7 +156,7 @@ var Dataset = (function() { // terms start with the same letter utils.each(terms, function(i, term) { var firstChar = term.charAt(0); - !~firstChars.indexOf(firstChar) && firstChars.push(firstChar); + !~utils.indexOf(firstChars, firstChar) && firstChars.push(firstChar); }); utils.each(firstChars, function(i, firstChar) { diff --git a/src/input_view.js b/src/input_view.js index b76f907..dea34fe 100644 --- a/src/input_view.js +++ b/src/input_view.js @@ -45,7 +45,7 @@ var InputView = (function() { // give the browser a chance to update the value of the input // before checking to see if the query changed - setTimeout(that._compareQueryToInputValue, 0); + utils.defer(that._compareQueryToInputValue); }); } diff --git a/src/typeahead.js b/src/typeahead.js index 062f4a9..401022e 100644 --- a/src/typeahead.js +++ b/src/typeahead.js @@ -47,7 +47,11 @@ })); $.when.apply($, deferreds) - .always(function() { eventBus.trigger('initialized'); }); + .always(function() { + // deferring to make it possible to attach a listener + // for typeahead:initialized after calling jQuery#typeahead + utils.defer(function() { eventBus.trigger('initialized'); }); + }); } }, diff --git a/src/typeahead_view.js b/src/typeahead_view.js index 2170fcc..b961b9d 100644 --- a/src/typeahead_view.js +++ b/src/typeahead_view.js @@ -215,7 +215,7 @@ var TypeaheadView = (function() { // focus is not a synchronous event in ie, so we deal with it byClick && utils.isMsie() ? - setTimeout(this.dropdownView.close, 0) : this.dropdownView.close(); + utils.defer(this.dropdownView.close) : this.dropdownView.close(); this.eventBus.trigger('selected', suggestion); } diff --git a/src/utils.js b/src/utils.js index b631dc0..e66c3a1 100644 --- a/src/utils.js +++ b/src/utils.js @@ -88,6 +88,8 @@ var utils = { return function() { return counter++; }; })(), + defer: function(fn) { setTimeout(fn, 0); }, + debounce: function(func, wait, immediate) { var timeout, result; diff --git a/test/playground.html b/test/playground.html index 108f990..1eb4079 100644 --- a/test/playground.html +++ b/test/playground.html @@ -50,13 +50,6 @@