Skip to content

Commit fb2f3d2

Browse files
committed
Widget: Enable namespace functions for widgets.
Example: $(".tab").custom().tab(); will use the widget defined by $.widget("custom.tab", {});
1 parent da8dfa5 commit fb2f3d2

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

tests/unit/widget/widget_core.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ test( "widget namespaces", function() {
4040
$.custom.testWidget.prototype._create = function(){ which = "custom"; };
4141
var elem3 = $( "<div></div>" ).custom_testWidget();
4242
equals( which, "custom", "creating a namespaced widget makes the correct one" );
43+
44+
// Now make sure we can explicitly pick the one we want via the chained version of namespace.
45+
var which = "neither";
46+
$.ui.testWidget.prototype._create = function(){ which = "ui"; };
47+
var elem2 = $( "<div></div>" ).ui().testWidget();
48+
equals( which, "ui" , "creating a namespaced widget with the namespace function makes the correct one" );
49+
50+
which = "neither";
51+
$.custom.testWidget.prototype._create = function(){ which = "custom"; };
52+
var elem3 = $( "<div></div>" ).custom().testWidget();
53+
equals( which, "custom", "creating a namespaced widget with the namespace function makes the correct one" );
54+
4355
});
4456

4557
test( "widget creation", function() {

ui/jquery.ui.widget.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,18 @@ $.widget.bridge = function( name, namespace, object ) {
127127
return returnValue;
128128
};
129129

130+
// Check to see if we aren't the first widget to come this way.
131+
if ( $.isFunction( $.fn[ namespace ] ) ) {
132+
$.fn[ namespace ][ name ] = bridgeFn;
133+
} else {
134+
// Store off the jquery instance so we can get at it in the namespaceFn
135+
var jquery_obj = this;
136+
var namespaceFn = function() {
137+
this[ name ] = bridgeFn;
138+
return this;
139+
}
140+
$.fn[ namespace ] = namespaceFn;
141+
}
130142

131143
// Attach the bridge function to both the raw name - example: `$.sortable()`
132144
// And also attach it to a namespaced name - example: `$.ui_sortable()`

0 commit comments

Comments
 (0)