From cc90b440607a1af87c4abb8b2ee3325e96b0f5a1 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Scott=20Gonz=C3=A1lez?= Date: Mon, 24 Jan 2011 13:33:24 -0500 Subject: [PATCH] Widget: Allow this.element to be the widget instance instead of a DOM element. Fixes #6895 - Widget: Allow non-DOM based widget. --- tests/unit/widget/widget_core.js | 32 +++++++++++++++++++++++++++++++- ui/jquery.ui.widget.js | 2 +- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/tests/unit/widget/widget_core.js b/tests/unit/widget/widget_core.js index 189b75368..269b897c5 100644 --- a/tests/unit/widget/widget_core.js +++ b/tests/unit/widget/widget_core.js @@ -27,7 +27,7 @@ test( "widget creation", function() { }); test( "element normalization", function() { - expect( 10 ); + expect( 12 ); var elem; $.widget( "ui.testWidget", {} ); @@ -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() { @@ -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", { diff --git a/ui/jquery.ui.widget.js b/ui/jquery.ui.widget.js index f6089519b..7dfcb4447 100644 --- a/ui/jquery.ui.widget.js +++ b/ui/jquery.ui.widget.js @@ -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, {}, -- 2.39.5