Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defer on triggering typeahead:initialized #117

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/input_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}

Expand Down
6 changes: 5 additions & 1 deletion src/typeahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'); });
});
}
},

Expand Down
2 changes: 1 addition & 1 deletion src/typeahead_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 2 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ var utils = {
return function() { return counter++; };
})(),

defer: function(fn) { setTimeout(fn, 0); },

debounce: function(func, wait, immediate) {
var timeout, result;

Expand Down
14 changes: 7 additions & 7 deletions test/playground.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,6 @@
</div>

<script>
$('input').on([
'typeahead:initialized',
'typeahead:selected',
'typeahead:opened',
'typeahead:closed'
].join(' '), logToTextarea);

$('.states').typeahead({
local: [
"Alabama",
Expand Down Expand Up @@ -161,6 +154,13 @@
}
]);

$('input').on([
'typeahead:initialized',
'typeahead:selected',
'typeahead:opened',
'typeahead:closed'
].join(' '), logToTextarea);

function logToTextarea($e) {
var $textarea = $('.triggered-events'),
val = $textarea.val(),
Expand Down