@@ -215,35 +215,42 @@ $.widget.bridge = function( name, object ) {
215215 var returnValue = this ;
216216
217217 if ( isMethodCall ) {
218- this . each ( function ( ) {
219- var methodValue ;
220- var instance = $ . data ( this , fullName ) ;
221218
222- if ( options === "instance" ) {
223- returnValue = instance ;
224- return false ;
225- }
219+ // If this is an empty collection, we need to have the instance method
220+ // return undefined instead of the jQuery instance
221+ if ( ! this . length && options === "instance" ) {
222+ returnValue = undefined ;
223+ } else {
224+ this . each ( function ( ) {
225+ var methodValue ;
226+ var instance = $ . data ( this , fullName ) ;
226227
227- if ( ! instance ) {
228- return $ . error ( "cannot call methods on " + name +
229- " prior to initialization; " +
230- "attempted to call method '" + options + "'" ) ;
231- }
228+ if ( options === "instance" ) {
229+ returnValue = instance ;
230+ return false ;
231+ }
232232
233- if ( ! $ . isFunction ( instance [ options ] ) || options . charAt ( 0 ) === "_" ) {
234- return $ . error ( "no such method '" + options + "' for " + name +
235- " widget instance" ) ;
236- }
233+ if ( ! instance ) {
234+ return $ . error ( "cannot call methods on " + name +
235+ " prior to initialization; " +
236+ "attempted to call method '" + options + "'" ) ;
237+ }
237238
238- methodValue = instance [ options ] . apply ( instance , args ) ;
239+ if ( ! $ . isFunction ( instance [ options ] ) || options . charAt ( 0 ) === "_" ) {
240+ return $ . error ( "no such method '" + options + "' for " + name +
241+ " widget instance" ) ;
242+ }
239243
240- if ( methodValue !== instance && methodValue !== undefined ) {
241- returnValue = methodValue && methodValue . jquery ?
242- returnValue . pushStack ( methodValue . get ( ) ) :
243- methodValue ;
244- return false ;
245- }
246- } ) ;
244+ methodValue = instance [ options ] . apply ( instance , args ) ;
245+
246+ if ( methodValue !== instance && methodValue !== undefined ) {
247+ returnValue = methodValue && methodValue . jquery ?
248+ returnValue . pushStack ( methodValue . get ( ) ) :
249+ methodValue ;
250+ return false ;
251+ }
252+ } ) ;
253+ }
247254 } else {
248255
249256 // Allow multiple hashes to be passed on init
0 commit comments