diff --git a/src/js/select2/data/tokenizer.js b/src/js/select2/data/tokenizer.js index 4a0faef6..77576556 100644 --- a/src/js/select2/data/tokenizer.js +++ b/src/js/select2/data/tokenizer.js @@ -21,6 +21,29 @@ define([ Tokenizer.prototype.query = function (decorated, params, callback) { var self = this; + function createAndSelect (data) { + // Normalize the data object so we can use it for checks + var item = self._normalizeItem(data); + + // Check if the data object already exists as a tag + // Select it if it doesn't + var $existingOptions = self.$element.find('option').filter(function () { + return $(this).val() === item.id; + }); + + // If an existing option wasn't found for it, create the option + if (!$existingOptions.length) { + var $option = self.option(item); + $option.attr('data-select2-tag', true); + + self._removeOldTags(); + self.addOptions([$option]); + } + + // Select the item, now that we know there is an option for it + select(item); + } + function select (data) { self.trigger('select', { data: data @@ -29,7 +52,7 @@ define([ params.term = params.term || ''; - var tokenData = this.tokenizer(params, this.options, select); + var tokenData = this.tokenizer(params, this.options, createAndSelect); if (tokenData.term !== params.term) { // Replace the search term if we have the search box