diff --git a/lib/ajax-chosen.js b/lib/ajax-chosen.js
index 4abf68b..a5e118a 100644
--- a/lib/ajax-chosen.js
+++ b/lib/ajax-chosen.js
@@ -98,6 +98,12 @@
if (data == null) {
return;
}
+ items = this.callback_function != null ? this.callback_function(data, this.search_field) : data;
+ if (!items.length) {
+ this.element.data().chosen.no_results_clear();
+ this.element.data().chosen.no_results(this.search_field.val());
+ return;
+ }
selected_values = [];
this.element.find('option').each(function() {
if (!$(this).is(":selected")) {
@@ -109,7 +115,6 @@
this.element.find('optgroup:empty').each(function() {
return $(this).remove();
});
- items = this.callback_function != null ? this.callback_function(data, this.search_field) : data;
nbItems = 0;
_this = this;
$.each(items, function(i, element) {
@@ -147,14 +152,9 @@
}
}
});
- if (nbItems) {
- val_before_trigger = this.search_field.val();
- this.element.trigger("chosen:updated");
- this.search_field.val(val_before_trigger);
- } else {
- this.element.data().chosen.no_results_clear();
- this.element.data().chosen.no_results(this.search_field.val());
- }
+ val_before_trigger = this.search_field.val();
+ this.element.trigger("chosen:updated");
+ this.search_field.val(val_before_trigger);
if (this.success != null) {
return this.success(data);
}
diff --git a/lib/ajax-chosen.min.js b/lib/ajax-chosen.min.js
index a8eec56..f3aaf63 100644
--- a/lib/ajax-chosen.min.js
+++ b/lib/ajax-chosen.min.js
@@ -9,10 +9,10 @@ this.options.data[this.options.jsonTermKey]=val;if(this.options.dataCallback!=nu
_this=this;options=this.options;options.success=function(data){return _this.show_results(data);};return this.timer=setTimeout(function(){if(_this.chosenXhr){_this.chosenXhr.abort();}
return _this.chosenXhr=$.ajax(options);},options.afterTypeDelay);};ajaxChosen.prototype.show_results=function(data){var _this,items,nbItems,selected_values,val_before_trigger;if(this.is_typing){return;}
if(data==null){return;}
-selected_values=[];this.element.find('option').each(function(){if(!$(this).is(":selected")){return $(this).remove();}else{return selected_values.push($(this).val()+"-"+$(this).text());}});this.element.find('optgroup:empty').each(function(){return $(this).remove();});items=this.callback_function!=null?this.callback_function(data,this.search_field):data;nbItems=0;_this=this;$.each(items,function(i,element){var group,text,value;nbItems++;if(element.group){group=_this.element.find("optgroup[label='"+element.text+"']");if(!group.size()){group=$("");}
+items=this.callback_function!=null?this.callback_function(data,this.search_field):data;if(!items.length){this.element.data().chosen.no_results_clear();this.element.data().chosen.no_results(this.search_field.val());return;}
+selected_values=[];this.element.find('option').each(function(){if(!$(this).is(":selected")){return $(this).remove();}else{return selected_values.push($(this).val()+"-"+$(this).text());}});this.element.find('optgroup:empty').each(function(){return $(this).remove();});nbItems=0;_this=this;$.each(items,function(i,element){var group,text,value;nbItems++;if(element.group){group=_this.element.find("optgroup[label='"+element.text+"']");if(!group.size()){group=$("");}
group.attr('label',element.text).appendTo(_this.element);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 $("").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 $("").attr('value',value).html(text).appendTo(_this.element);}}});if(nbItems){val_before_trigger=this.search_field.val();this.element.trigger("chosen:updated");this.search_field.val(val_before_trigger);}else{this.element.data().chosen.no_results_clear();this.element.data().chosen.no_results(this.search_field.val());}
-if(this.success!=null){return this.success(data);}};$.fn.ajaxChosen=function(options,callback,chosenOptions){if(options==null){options={};}
+if($.inArray(value+"-"+text,selected_values)===-1){return $("").attr('value',value).html(text).appendTo(_this.element);}}});val_before_trigger=this.search_field.val();this.element.trigger("chosen:updated");this.search_field.val(val_before_trigger);if(this.success!=null){return this.success(data);}};$.fn.ajaxChosen=function(options,callback,chosenOptions){if(options==null){options={};}
if(chosenOptions==null){chosenOptions={};}
return this.each(function(){return new ajaxChosen($(this),options,callback,chosenOptions);});};return ajaxChosen;})();})(jQuery);
\ No newline at end of file
diff --git a/src/ajax-chosen.coffee b/src/ajax-chosen.coffee
index fd7fd6b..4727da0 100644
--- a/src/ajax-chosen.coffee
+++ b/src/ajax-chosen.coffee
@@ -100,6 +100,15 @@ do ($ = jQuery) ->
# Exit if the data we're given is invalid
return unless data?
+ # Send the ajax results to the user callback so we can get an object of
+ # value => text pairs to inject as