From c24f7e113af32921a8fb8a9b9f9f76941f365572 Mon Sep 17 00:00:00 2001 From: Kamal Fariz Mahyuddin Date: Fri, 5 Aug 2011 21:48:39 +0800 Subject: [PATCH 1/3] Guard against adding already selected options --- coffee/chosen.jquery.coffee | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/coffee/chosen.jquery.coffee b/coffee/chosen.jquery.coffee index d7ad36b49a7..9b6188ed932 100644 --- a/coffee/chosen.jquery.coffee +++ b/coffee/chosen.jquery.coffee @@ -15,7 +15,7 @@ $.fn.extend({ class Chosen constructor: (elmn, data, options) -> - @options = $.extend({}, options); + @options = $.extend({}, options) this.set_default_values() @form_field = elmn @@ -465,24 +465,29 @@ class Chosen this.result_do_highlight do_high if do_high? no_results: (terms) -> - no_results_html = $('
  • No results match "". Add this item
  • ') + no_results_html = $('
  • No results match "".
  • ') no_results_html.find("span").first().html(terms) - no_results_html.find("a.option-add").bind "click", (evt) => this.select_add_option(terms) + + regex = new RegExp('^' + terms + '$', 'i') + selected = (option for option in @results_data when regex.test(option.value) and option.selected) + if (selected.length == 0) + no_results_html.append(' Add this item') + no_results_html.find("a.option-add").bind "click", (evt) => this.select_add_option(terms) @search_results.append no_results_html - + select_add_option: (terms) -> - if $.isFunction(@options.addOption) - @options.addOption(terms); + if $.isFunction(@options.addOption) + @options.addOption(terms) else new_option_html = $('