@@ -100,6 +100,15 @@ do ($ = jQuery) ->
100
100
# Exit if the data we're given is invalid
101
101
return unless data?
102
102
103
+ # Send the ajax results to the user callback so we can get an object of
104
+ # value => text pairs to inject as <option> elements.
105
+ items = if @callback_function ? then @ callback_function (data, @search_field ) else data
106
+ unless items .length
107
+ # If there are no results, display the no_results text
108
+ @element .data ().chosen .no_results_clear ()
109
+ @element .data ().chosen .no_results @search_field .val ()
110
+ return
111
+
103
112
# Go through all of the <option> elements in the <select> and remove
104
113
# ones that have not been selected by the user. For those selected
105
114
# by the user, add them to a list to filter from the results later.
@@ -112,10 +121,6 @@ do ($ = jQuery) ->
112
121
@element .find (' optgroup:empty' ).each ->
113
122
$ (@ ).remove ()
114
123
115
- # Send the ajax results to the user callback so we can get an object of
116
- # value => text pairs to inject as <option> elements.
117
- items = if @callback_function ? then @ callback_function (data, @search_field ) else data
118
-
119
124
nbItems = 0
120
125
121
126
_this = @
@@ -156,20 +161,15 @@ do ($ = jQuery) ->
156
161
.html (text)
157
162
.appendTo (_this .element )
158
163
159
- if nbItems
160
- # Tell chosen that the contents of the <select> input have been updated
161
- # This makes chosen update its internal list of the input data.
162
- val_before_trigger = @search_field .val ()
163
- @element .trigger (" chosen:updated" )
164
- # For some reason, the contents of the input field get removed once you
165
- # call trigger above. Often, this can be very annoying (and can make some
166
- # searches impossible), so we add the value the user was typing back into
167
- # the input field.
168
- @search_field .val (val_before_trigger)
169
- else
170
- # If there are no results, display the no_results text
171
- @element .data ().chosen .no_results_clear ()
172
- @element .data ().chosen .no_results @search_field .val ()
164
+ # Tell chosen that the contents of the <select> input have been updated
165
+ # This makes chosen update its internal list of the input data.
166
+ val_before_trigger = @search_field .val ()
167
+ @element .trigger (" chosen:updated" )
168
+ # For some reason, the contents of the input field get removed once you
169
+ # call trigger above. Often, this can be very annoying (and can make some
170
+ # searches impossible), so we add the value the user was typing back into
171
+ # the input field.
172
+ @search_field .val (val_before_trigger)
173
173
174
174
# Finally, call the user supplied callback (if it exists)
175
175
@ success (data) if @success ?
0 commit comments