Skip to content

Commit cc90b44

Browse files
committed
Widget: Allow this.element to be the widget instance instead of a DOM element. Fixes #6895 - Widget: Allow non-DOM based widget.
1 parent bc71499 commit cc90b44

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

tests/unit/widget/widget_core.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ test( "widget creation", function() {
2727
});
2828

2929
test( "element normalization", function() {
30-
expect( 10 );
30+
expect( 12 );
3131
var elem;
3232
$.widget( "ui.testWidget", {} );
3333

@@ -65,6 +65,14 @@ test( "element normalization", function() {
6565
same( elem.data( "testWidget" ), this, "instace stored in .data()" );
6666
};
6767
$.ui.testWidget( {}, "#element-normalization-selector" );
68+
69+
$.ui.testWidget.prototype.defaultElement = null;
70+
$.ui.testWidget.prototype._create = function() {
71+
// using strictEqual throws an error (Maximum call stack size exceeded)
72+
ok( this.element[ 0 ] === this, "instance as element" );
73+
ok( this.element.data( "testWidget" ) === this, "instance stored in .data()" );
74+
};
75+
$.ui.testWidget();
6876
});
6977

7078
test( "jQuery usage", function() {
@@ -573,6 +581,28 @@ test( "._trigger() - provide event and ui", function() {
573581
.testWidget( "testEvent" );
574582
});
575583

584+
test( "._triger() - instance as element", function() {
585+
expect( 4 );
586+
$.widget( "ui.testWidget", {
587+
defaultElement: null,
588+
testEvent: function() {
589+
var ui = { foo: "bar" };
590+
this._trigger( "foo", null, ui );
591+
}
592+
});
593+
var instance = $.ui.testWidget({
594+
foo: function( event, ui ) {
595+
equal( event.type, "testwidgetfoo", "event object passed to callback" );
596+
same( ui, { foo: "bar" }, "ui object passed to callback" );
597+
}
598+
});
599+
$( instance ).bind( "testwidgetfoo", function( event, ui ) {
600+
equal( event.type, "testwidgetfoo", "event object passed to event handler" );
601+
same( ui, { foo: "bar" }, "ui object passed to event handler" );
602+
});
603+
instance.testEvent();
604+
});
605+
576606
test( "auto-destroy - .remove()", function() {
577607
expect( 1 );
578608
$.widget( "ui.testWidget", {

ui/jquery.ui.widget.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ $.Widget.prototype = {
133133
disabled: false
134134
},
135135
_createWidget: function( options, element ) {
136-
element = $( element || this.defaultElement )[ 0 ];
136+
element = $( element || this.defaultElement || this )[ 0 ];
137137
$.data( element, this.widgetName, this );
138138
this.element = $( element );
139139
this.options = $.extend( true, {},

0 commit comments

Comments
 (0)