Skip to content
Browse files

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

  • Loading branch information...
1 parent fea825d commit 35d9face9b12cbdde432b115bfb55d6e3545ca46 @michaelperrin michaelperrin committed Oct 9, 2012
Showing with 45 additions and 15 deletions.
  1. +23 −6 lib/ajax-chosen.js
  2. +1 −1 lib/ajax-chosen.min.js
  3. +21 −8 src/ajax-chosen.coffee
View
29 lib/ajax-chosen.js
@@ -65,16 +65,33 @@
});
items = callback(data);
$.each(items, function(i, element) {
- var group;
+ var group, text, value;
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);
+ 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 ($.inArray(element.value + "-" + element.text, selected_values) === -1) {
- return $("<option />").attr('value', element.value).html(element.text).appendTo(select);
+ } 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");
@@ -92,4 +109,4 @@
});
});
};
-})(jQuery);
+})(jQuery);
View
2 lib/ajax-chosen.min.js
@@ -8,6 +8,6 @@ if(val.length<options.minTermLength){return false;}
field=$(this);if(!(options.data!=null)){options.data={};}
options.data[options.jsonTermKey]=val;if(options.dataCallback!=null){options.data=options.dataCallback(options.data);}
success=options.success;options.success=function(data){var items,selected_values;if(!(data!=null)){return;}
-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);}
+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);}
return field.attr('value',untrimmed_val);};return this.timer=setTimeout(function(){if(chosenXhr){chosenXhr.abort();}
return chosenXhr=$.ajax(options);},options.afterTypeDelay);});});};})(jQuery);
View
29 src/ajax-chosen.coffee
@@ -99,16 +99,29 @@ do ($ = jQuery) ->
.attr('label', element.text)
.appendTo(select)
$.each element.items, (i, element) ->
- if $.inArray(element.value + "-" + element.text, selected_values) == -1
+ if typeof element == "string"
+ value = i;
+ text = element;
+ else
+ value = element.value;
+ text = element.text;
+ if $.inArray(value + "-" + text, selected_values) == -1
$("<option />")
- .attr('value', element.value)
- .html(element.text)
+ .attr('value', value)
+ .html(text)
.appendTo(group)
- else if $.inArray(element.value + "-" + element.text, selected_values) == -1
- $("<option />")
- .attr('value', element.value)
- .html(element.text)
- .appendTo(select)
+ else
+ if typeof element == "string"
+ value = i;
+ text = element;
+ else
+ value = element.value;
+ text = element.text;
+ if $.inArray(value + "-" + text, selected_values) == -1
+ $("<option />")
+ .attr('value', value)
+ .html(text)
+ .appendTo(select)
# Tell chosen that the contents of the <select> input have been updated
# 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.