Created
November 5, 2012 19:08
-
-
Save Avinash-Bhat/4019648 to your computer and use it in GitHub Desktop.
fix for problematic bug in jquery ui 1.9.1 (ticket#8775)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.widget.bridge = function( name, object ) { | |
var fullName = object.prototype.widgetFullName || name; | |
$.fn[ name ] = function( options ) { | |
var isMethodCall = typeof options === "string", | |
args = slice.call( arguments, 1 ), | |
returnValue = this; | |
// allow multiple hashes to be passed on init | |
options = !isMethodCall && args.length ? | |
$.widget.extend.apply( null, [ options ].concat(args) ) : | |
options; | |
if ( isMethodCall ) { | |
this.each(function() { | |
var methodValue, | |
instance = $.data( this, fullName ); | |
if ( !instance ) { | |
return $.error( "cannot call methods on " + name + " prior to initialization; " + | |
"attempted to call method '" + options + "'" ); | |
} | |
if ( !$.isFunction( instance[options] ) || options.charAt( 0 ) === "_" ) { | |
return $.error( "no such method '" + options + "' for " + name + " widget instance" ); | |
} | |
methodValue = instance[ options ].apply( instance, args ); | |
if ( methodValue !== instance && methodValue !== undefined ) { | |
returnValue = methodValue && methodValue.jquery ? | |
returnValue.pushStack( methodValue.get() ) : | |
methodValue; | |
return false; | |
} | |
}); | |
} else { | |
this.each(function() { | |
var instance = $.data( this, fullName ); | |
if ( instance ) { | |
instance.option( options || {} )._init(); | |
} else { | |
$.data( this, fullName, new object( options, this ) ); | |
} | |
}); | |
} | |
return returnValue; | |
}; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
test( "bridge - widget-creation", function() { | |
expect( 2 ); | |
function MyPrototype(option, element) {} | |
MyPrototype.prototype = { | |
_create: function() {}, | |
creationTest: function() {} | |
}; | |
$.widget.bridge( "testWidget", MyPrototype ); | |
ok( $.isFunction( $.fn.testWidget ), "constructor was created" ); | |
equal( "object", typeof $.fn.testWidget.prototype, "prototype was created" ); | |
}); | |
test( "bridge - jQuery usage", function() { | |
expect( 13 ); | |
var elem, instance, ret, | |
shouldCreate = false; | |
function MyPrototype( options, element ) { | |
this.element = $( element ); | |
this.options = options; | |
ok( shouldCreate, "create called on instantiation" ); | |
} | |
MyPrototype.prototype = { | |
getterSetterVal: 5, | |
methodWithParams: function( param1, param2 ) { | |
ok( true, "method called via .pluginName(methodName)" ); | |
equal( param1, "value1", | |
"parameter passed via .pluginName(methodName, param)" ); | |
equal( param2, "value2", | |
"multiple parameters passed via .pluginName(methodName, param, param)" ); | |
return this; | |
}, | |
getterSetterMethod: function( val ) { | |
if ( val ) { | |
this.getterSetterVal = val; | |
} else { | |
return this.getterSetterVal; | |
} | |
}, | |
jQueryObject: function() { | |
return $( "body" ); | |
}, | |
destroy: function() { | |
this.element.removeData( "testWidget" ); | |
delete this.element; | |
delete this.options; | |
} | |
}; | |
$.widget.bridge( "testWidget", MyPrototype ); | |
shouldCreate = true; | |
elem = $( "<div>" ).testWidget(); | |
shouldCreate = false; | |
instance = elem.data( "testWidget" ); | |
equal( typeof instance, "object", "instance stored in .data(pluginName)" ); | |
equal( instance.element[0], elem[0], "element stored on widget" ); | |
ret = elem.testWidget( "methodWithParams", "value1", "value2" ); | |
equal( ret, elem, "jQuery object returned from method call" ); | |
ret = elem.testWidget( "getterSetterMethod" ); | |
equal( ret, 5, "getter/setter can act as getter" ); | |
ret = elem.testWidget( "getterSetterMethod", 30 ); | |
equal( ret, elem, "getter/setter method can be chainable" ); | |
equal( instance.getterSetterVal, 30, "getter/setter can act as setter" ); | |
ret = elem.testWidget( "jQueryObject" ); | |
equal( ret[ 0 ], document.body, "returned jQuery object" ); | |
equal( ret.end(), elem, "stack preserved" ); | |
elem.testWidget( "destroy" ); | |
equal( elem.data( "testWidget" ), null, "completely destroyed" ); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment