@@ -66,6 +66,9 @@ return $.widget( "ui.selectmenu", {
6666this . _drawButton ( ) ;
6767this . _drawMenu ( ) ;
6868
69+ this . _rendered = false ;
70+ this . menuItems = $ ( ) ;
71+
6972if ( this . options . disabled ) {
7073this . disable ( ) ;
7174}
@@ -119,7 +122,7 @@ return $.widget( "ui.selectmenu", {
119122
120123// Delay rendering the menu items until the button receives focus.
121124// The menu may have already been rendered via a programmatic open.
122- if ( ! that . menuItems ) {
125+ if ( ! that . _rendered ) {
123126that . _refreshMenu ( ) ;
124127}
125128} ) ;
@@ -199,7 +202,9 @@ return $.widget( "ui.selectmenu", {
199202this . _refreshMenu ( ) ;
200203this . buttonItem . replaceWith (
201204this . 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" ) || { }
203208)
204209) ;
205210if ( ! this . options . width ) {
@@ -208,14 +213,10 @@ return $.widget( "ui.selectmenu", {
208213} ,
209214
210215_refreshMenu : function ( ) {
211- this . menu . empty ( ) ;
212-
213216var item ,
214217options = this . element . find ( "option" ) ;
215218
216- if ( ! options . length ) {
217- return ;
218- }
219+ this . menu . empty ( ) ;
219220
220221this . _parseOptions ( options ) ;
221222this . _renderMenu ( this . menu , this . items ) ;
@@ -225,6 +226,12 @@ return $.widget( "ui.selectmenu", {
225226. not ( ".ui-selectmenu-optgroup" )
226227. find ( ".ui-menu-item-wrapper" ) ;
227228
229+ this . _rendered = true ;
230+
231+ if ( ! options . length ) {
232+ return ;
233+ }
234+
228235item = this . _getSelectedItem ( ) ;
229236
230237// Update the menu to have the correct item focused
@@ -241,7 +248,7 @@ return $.widget( "ui.selectmenu", {
241248}
242249
243250// If this is the first time the menu is being opened, render the items
244- if ( ! this . menuItems ) {
251+ if ( ! this . _rendered ) {
245252this . _refreshMenu ( ) ;
246253} else {
247254
@@ -250,6 +257,11 @@ return $.widget( "ui.selectmenu", {
250257this . menuInstance . focus ( null , this . _getSelectedItem ( ) ) ;
251258}
252259
260+ // If there are no options, don't open the menu
261+ if ( ! this . menuItems . length ) {
262+ return ;
263+ }
264+
253265this . isOpen = true ;
254266this . _toggleAttr ( ) ;
255267this . _resizeMenu ( ) ;
0 commit comments