Skip to content

Commit da8dfa5

Browse files
committed
Widget: Tests & Impl of namespaced widgets with underscores.
Example: cb_widget(); to access the widget defined by $.widget(cb.widget, {});
1 parent d7670b9 commit da8dfa5

File tree

2 files changed

+76
-6
lines changed

2 files changed

+76
-6
lines changed

tests/unit/widget/widget_core.js

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,39 @@ module( "widget factory", {
99
}
1010
});
1111

12+
test( "widget namespaces", function() {
13+
var fail = function(msg) { ok(false, msg); };
14+
15+
var uiPrototype = {
16+
_create: function() { fail( "This widget should not have been instantiated" ); }
17+
};
18+
19+
var customPrototype = {
20+
_create: function() { ok( true, "Second defined widget is the one that takes over default scope" ) }
21+
}
22+
23+
$.widget( "ui.testWidget", uiPrototype );
24+
$.widget( "custom.testWidget", customPrototype );
25+
26+
var elem = $( "<div></div>" ).testWidget();
27+
28+
ok( $.isFunction( $.ui.testWidget ), "constructor was created in the ui namespace" );
29+
ok( $.isFunction( $.custom.testWidget ), "constructor was created in the custom namespace" );
30+
equals( uiPrototype._create, $.ui.testWidget.prototype._create, "ui widget is in the ui namespace" );
31+
equals( customPrototype._create, $.custom.testWidget.prototype._create, "custom namespace is maintained" );
32+
33+
// Now make sure we can explicitly pick the one we want
34+
var which = "neither";
35+
$.ui.testWidget.prototype._create = function(){ which = "ui"; };
36+
var elem2 = $( "<div></div>" ).ui_testWidget();
37+
equals( which, "ui" , "creating a namespaced widget makes the correct one" );
38+
39+
which = "neither";
40+
$.custom.testWidget.prototype._create = function(){ which = "custom"; };
41+
var elem3 = $( "<div></div>" ).custom_testWidget();
42+
equals( which, "custom", "creating a namespaced widget makes the correct one" );
43+
});
44+
1245
test( "widget creation", function() {
1346
var myPrototype = {
1447
_create: function() {},
@@ -236,7 +269,35 @@ test( ".option() - delegate to ._setOptions()", function() {
236269
"_setOptions called with multiple options" );
237270
});
238271

239-
test( ".option() - delegate to ._setOption()", function() {
272+
test( ".option() - getter - custom namespace", function() {
273+
$.widget( "custom.testWidget", {
274+
_create: function() {}
275+
});
276+
277+
var div = $( "<div></div>" ).custom_testWidget({
278+
foo: "bar",
279+
baz: 5,
280+
qux: [ "quux", "quuux" ]
281+
});
282+
283+
same( div.custom_testWidget( "option", "foo"), "bar", "single option - string" );
284+
same( div.custom_testWidget( "option", "baz"), 5, "single option - number" );
285+
same( div.custom_testWidget( "option", "qux"), [ "quux", "quuux" ],
286+
"single option - array" );
287+
288+
var options = div.custom_testWidget( "option" );
289+
same( options, {
290+
disabled: false,
291+
foo: "bar",
292+
baz: 5,
293+
qux: [ "quux", "quuux" ]
294+
}, "full options hash returned" );
295+
options.foo = "notbar";
296+
same( div.custom_testWidget( "option", "foo"), "bar",
297+
"modifying returned options hash does not modify plugin instance" );
298+
});
299+
300+
test( ".option() - setter", function() {
240301
var calls = [];
241302
$.widget( "ui.testWidget", {
242303
_create: function() {},

ui/jquery.ui.widget.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,11 @@ $.widget = function( name, base, prototype ) {
7575
widgetBaseClass: fullName
7676
}, prototype );
7777

78-
$.widget.bridge( name, $[ namespace ][ name ] );
78+
$.widget.bridge( name, namespace, $[ namespace ][ name ] );
7979
};
8080

81-
$.widget.bridge = function( name, object ) {
82-
$.fn[ name ] = function( options ) {
81+
$.widget.bridge = function( name, namespace, object ) {
82+
var bridgeFn = function( options ) {
8383
var isMethodCall = typeof options === "string",
8484
args = Array.prototype.slice.call( arguments, 1 ),
8585
returnValue = this;
@@ -111,18 +111,27 @@ $.widget.bridge = function( name, object ) {
111111
}
112112
});
113113
} else {
114+
// Make each element of the jquery list an instance of this widget
114115
this.each(function() {
115-
var instance = $.data( this, name );
116+
var instance = $.data( this, namespace + "." + name );
116117
if ( instance ) {
117118
instance.option( options || {} )._init();
118119
} else {
119-
$.data( this, name, new object( options, this ) );
120+
var instance = new object( options, this );
121+
$.data( this, name, instance );
122+
$.data( this, namespace + "." + name, instance );
120123
}
121124
});
122125
}
123126

124127
return returnValue;
125128
};
129+
130+
131+
// Attach the bridge function to both the raw name - example: `$.sortable()`
132+
// And also attach it to a namespaced name - example: `$.ui_sortable()`
133+
$.fn[ name ] = bridgeFn;
134+
$.fn[ namespace + "_" + name ] = bridgeFn;
126135
};
127136

128137
$.Widget = function( options, element ) {

0 commit comments

Comments
 (0)