Skip to content
This repository
Browse code

Do not break compatibility (old format of callback is valid as well)

  • Loading branch information...
commit 35d9face9b12cbdde432b115bfb55d6e3545ca46 1 parent fea825d
Michaël Perrin michaelperrin authored
29 lib/ajax-chosen.js
@@ -65,16 +65,33 @@
65 65 });
66 66 items = callback(data);
67 67 $.each(items, function(i, element) {
68   - var group;
  68 + var group, text, value;
69 69 if (element.group) {
70 70 group = $("<optgroup />").attr('label', element.text).appendTo(select);
71 71 return $.each(element.items, function(i, element) {
72   - if ($.inArray(element.value + "-" + element.text, selected_values) === -1) {
73   - return $("<option />").attr('value', element.value).html(element.text).appendTo(group);
  72 + var text, value;
  73 + if (typeof element === "string") {
  74 + value = i;
  75 + text = element;
  76 + } else {
  77 + value = element.value;
  78 + text = element.text;
  79 + }
  80 + if ($.inArray(value + "-" + text, selected_values) === -1) {
  81 + return $("<option />").attr('value', value).html(text).appendTo(group);
74 82 }
75 83 });
76   - } else if ($.inArray(element.value + "-" + element.text, selected_values) === -1) {
77   - return $("<option />").attr('value', element.value).html(element.text).appendTo(select);
  84 + } else {
  85 + if (typeof element === "string") {
  86 + value = i;
  87 + text = element;
  88 + } else {
  89 + value = element.value;
  90 + text = element.text;
  91 + }
  92 + if ($.inArray(value + "-" + text, selected_values) === -1) {
  93 + return $("<option />").attr('value', value).html(text).appendTo(select);
  94 + }
78 95 }
79 96 });
80 97 select.trigger("liszt:updated");
@@ -92,4 +109,4 @@
92 109 });
93 110 });
94 111 };
95   -})(jQuery);
  112 +})(jQuery);
2  lib/ajax-chosen.min.js
@@ -8,6 +8,6 @@ if(val.length<options.minTermLength){return false;}
8 8 field=$(this);if(!(options.data!=null)){options.data={};}
9 9 options.data[options.jsonTermKey]=val;if(options.dataCallback!=null){options.data=options.dataCallback(options.data);}
10 10 success=options.success;options.success=function(data){var items,selected_values;if(!(data!=null)){return;}
11   -selected_values=[];select.find('optgroup').each(function(){return $(this).remove();});select.find('option').each(function(){if(!$(this).is(":selected")){return $(this).remove();}else{return selected_values.push($(this).val()+"-"+$(this).text());}});items=callback(data);$.each(items,function(i,element){var group;if(element.group){group=$("<optgroup />").attr('label',element.text).appendTo(select);return $.each(element.items,function(i,element){if($.inArray(element.value+"-"+element.text,selected_values)===-1){return $("<option />").attr('value',element.value).html(element.text).appendTo(group);}});}else if($.inArray(element.value+"-"+element.text,selected_values)===-1){return $("<option />").attr('value',element.value).html(element.text).appendTo(select);}});select.trigger("liszt:updated");if(success!=null){success(data);}
  11 +selected_values=[];select.find("optgroup").each(function(){return $(this).remove();});select.find("option").each(function(){if(!$(this).is(":selected")){return $(this).remove();}else{return selected_values.push($(this).val()+"-"+$(this).text());}});items=callback(data);$.each(items,function(i,element){var group,text,value;if(element.group){group=$("<optgroup />").attr("label",element.text).appendTo(select);return $.each(element.items,function(i,element){var text,value;if(typeof element==="string"){value=i;text=element;}else{value=element.value;text=element.text;}if($.inArray(value+"-"+text,selected_values)===-1){return $("<option />").attr("value",value).html(text).appendTo(group);}});}else{if(typeof element==="string"){value=i;text=element;}else{value=element.value;text=element.text;}if($.inArray(value+"-"+text,selected_values)===-1){return $("<option />").attr("value",value).html(text).appendTo(select);}}});select.trigger("liszt:updated");if(success!=null){success(data);}
12 12 return field.attr('value',untrimmed_val);};return this.timer=setTimeout(function(){if(chosenXhr){chosenXhr.abort();}
13 13 return chosenXhr=$.ajax(options);},options.afterTypeDelay);});});};})(jQuery);
29 src/ajax-chosen.coffee
@@ -99,16 +99,29 @@ do ($ = jQuery) ->
99 99 .attr('label', element.text)
100 100 .appendTo(select)
101 101 $.each element.items, (i, element) ->
102   - if $.inArray(element.value + "-" + element.text, selected_values) == -1
  102 + if typeof element == "string"
  103 + value = i;
  104 + text = element;
  105 + else
  106 + value = element.value;
  107 + text = element.text;
  108 + if $.inArray(value + "-" + text, selected_values) == -1
103 109 $("<option />")
104   - .attr('value', element.value)
105   - .html(element.text)
  110 + .attr('value', value)
  111 + .html(text)
106 112 .appendTo(group)
107   - else if $.inArray(element.value + "-" + element.text, selected_values) == -1
108   - $("<option />")
109   - .attr('value', element.value)
110   - .html(element.text)
111   - .appendTo(select)
  113 + else
  114 + if typeof element == "string"
  115 + value = i;
  116 + text = element;
  117 + else
  118 + value = element.value;
  119 + text = element.text;
  120 + if $.inArray(value + "-" + text, selected_values) == -1
  121 + $("<option />")
  122 + .attr('value', value)
  123 + .html(text)
  124 + .appendTo(select)
112 125
113 126 # Tell chosen that the contents of the <select> input have been updated
114 127 # This makes chosen update its internal list of the input data.

0 comments on commit 35d9fac

Please sign in to comment.
Something went wrong with that request. Please try again.