Skip to content

Commit

Permalink
prevent extra events from being fired when autocompleting
Browse files Browse the repository at this point in the history
  • Loading branch information
aehlke committed Nov 23, 2012
1 parent df22bd8 commit 6999353
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions js/tag-it.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,29 +215,26 @@
that.tagInput.autocomplete('close');
}
}).blur(function(e){
// Create a tag when the element loses focus (unless it's empty).
that.createTag(that._cleanedInput());
// Create a tag when the element loses focus.
// If autocomplete is enabled and suggestion was clicked, don't add it.
if (!that.tagInput.data('autocomplete-open')) {
that.createTag(that._cleanedInput());
}
});


// Autocomplete.
if (this.options.availableTags || this.options.tagSource) {
this.tagInput.autocomplete({
source: this.options.tagSource,
select: function(event, ui) {
// Delete the last tag if we autocomplete something despite the input being empty
// This happens because the input's blur event causes the tag to be created when
// the user clicks an autocomplete item.
// The only artifact of this is that while the user holds down the mouse button
// on the selected autocomplete item, a tag is shown with the pre-autocompleted text,
// and is changed to the autocompleted text upon mouseup.
if (that.tagInput.val() === '') {
that.removeTag(that._lastTag(), false);
}
that.createTag(ui.item.value);
// Preventing the tag input to be updated with the chosen value.
return false;
}
}).bind('autocompleteopen', function(event, ui) {
that.tagInput.data('autocomplete-open', true);
}).bind('autocompleteclose', function(event, ui) {
that.tagInput.data('autocomplete-open', false)
});
}
},
Expand Down

0 comments on commit 6999353

Please sign in to comment.