Skip to content

Commit

Permalink
Widget: Allow this.element to be the widget instance instead of a DOM…
Browse files Browse the repository at this point in the history
… element. Fixes #6895 - Widget: Allow non-DOM based widget.
  • Loading branch information
scottgonzalez committed Jan 24, 2011
1 parent bc71499 commit cc90b44
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
32 changes: 31 additions & 1 deletion tests/unit/widget/widget_core.js
Expand Up @@ -27,7 +27,7 @@ test( "widget creation", function() {
});

test( "element normalization", function() {
expect( 10 );
expect( 12 );
var elem;
$.widget( "ui.testWidget", {} );

Expand Down Expand Up @@ -65,6 +65,14 @@ test( "element normalization", function() {
same( elem.data( "testWidget" ), this, "instace stored in .data()" );
};
$.ui.testWidget( {}, "#element-normalization-selector" );

$.ui.testWidget.prototype.defaultElement = null;
$.ui.testWidget.prototype._create = function() {
// using strictEqual throws an error (Maximum call stack size exceeded)
ok( this.element[ 0 ] === this, "instance as element" );
ok( this.element.data( "testWidget" ) === this, "instance stored in .data()" );
};
$.ui.testWidget();
});

test( "jQuery usage", function() {
Expand Down Expand Up @@ -573,6 +581,28 @@ test( "._trigger() - provide event and ui", function() {
.testWidget( "testEvent" );
});

test( "._triger() - instance as element", function() {
expect( 4 );
$.widget( "ui.testWidget", {
defaultElement: null,
testEvent: function() {
var ui = { foo: "bar" };
this._trigger( "foo", null, ui );
}
});
var instance = $.ui.testWidget({
foo: function( event, ui ) {
equal( event.type, "testwidgetfoo", "event object passed to callback" );
same( ui, { foo: "bar" }, "ui object passed to callback" );
}
});
$( instance ).bind( "testwidgetfoo", function( event, ui ) {
equal( event.type, "testwidgetfoo", "event object passed to event handler" );
same( ui, { foo: "bar" }, "ui object passed to event handler" );
});
instance.testEvent();
});

test( "auto-destroy - .remove()", function() {
expect( 1 );
$.widget( "ui.testWidget", {
Expand Down
2 changes: 1 addition & 1 deletion ui/jquery.ui.widget.js
Expand Up @@ -133,7 +133,7 @@ $.Widget.prototype = {
disabled: false
},
_createWidget: function( options, element ) {
element = $( element || this.defaultElement )[ 0 ];
element = $( element || this.defaultElement || this )[ 0 ];
$.data( element, this.widgetName, this );
this.element = $( element );
this.options = $.extend( true, {},
Expand Down

0 comments on commit cc90b44

Please sign in to comment.