@@ -66,6 +66,9 @@ return $.widget( "ui.selectmenu", {
66
66
this . _drawButton ( ) ;
67
67
this . _drawMenu ( ) ;
68
68
69
+ this . _rendered = false ;
70
+ this . menuItems = $ ( ) ;
71
+
69
72
if ( this . options . disabled ) {
70
73
this . disable ( ) ;
71
74
}
@@ -119,7 +122,7 @@ return $.widget( "ui.selectmenu", {
119
122
120
123
// Delay rendering the menu items until the button receives focus.
121
124
// The menu may have already been rendered via a programmatic open.
122
- if ( ! that . menuItems ) {
125
+ if ( ! that . _rendered ) {
123
126
that . _refreshMenu ( ) ;
124
127
}
125
128
} ) ;
@@ -199,7 +202,9 @@ return $.widget( "ui.selectmenu", {
199
202
this . _refreshMenu ( ) ;
200
203
this . buttonItem . replaceWith (
201
204
this . buttonItem = this . _renderButtonItem (
202
- this . _getSelectedItem ( ) . data ( "ui-selectmenu-item" )
205
+
206
+ // Fall back to an empty object in case there are no options
207
+ this . _getSelectedItem ( ) . data ( "ui-selectmenu-item" ) || { }
203
208
)
204
209
) ;
205
210
if ( ! this . options . width ) {
@@ -208,14 +213,10 @@ return $.widget( "ui.selectmenu", {
208
213
} ,
209
214
210
215
_refreshMenu : function ( ) {
211
- this . menu . empty ( ) ;
212
-
213
216
var item ,
214
217
options = this . element . find ( "option" ) ;
215
218
216
- if ( ! options . length ) {
217
- return ;
218
- }
219
+ this . menu . empty ( ) ;
219
220
220
221
this . _parseOptions ( options ) ;
221
222
this . _renderMenu ( this . menu , this . items ) ;
@@ -225,6 +226,12 @@ return $.widget( "ui.selectmenu", {
225
226
. not ( ".ui-selectmenu-optgroup" )
226
227
. find ( ".ui-menu-item-wrapper" ) ;
227
228
229
+ this . _rendered = true ;
230
+
231
+ if ( ! options . length ) {
232
+ return ;
233
+ }
234
+
228
235
item = this . _getSelectedItem ( ) ;
229
236
230
237
// Update the menu to have the correct item focused
@@ -241,7 +248,7 @@ return $.widget( "ui.selectmenu", {
241
248
}
242
249
243
250
// If this is the first time the menu is being opened, render the items
244
- if ( ! this . menuItems ) {
251
+ if ( ! this . _rendered ) {
245
252
this . _refreshMenu ( ) ;
246
253
} else {
247
254
@@ -250,6 +257,11 @@ return $.widget( "ui.selectmenu", {
250
257
this . menuInstance . focus ( null , this . _getSelectedItem ( ) ) ;
251
258
}
252
259
260
+ // If there are no options, don't open the menu
261
+ if ( ! this . menuItems . length ) {
262
+ return ;
263
+ }
264
+
253
265
this . isOpen = true ;
254
266
this . _toggleAttr ( ) ;
255
267
this . _resizeMenu ( ) ;
0 commit comments