diff options
author | Scott González <scott.gonzalez@gmail.com> | 2015-05-13 14:59:02 -0400 |
---|---|---|
committer | Scott González <scott.gonzalez@gmail.com> | 2015-05-14 20:36:00 -0400 |
commit | 556b2710f0f09b76909b92c751edc3f4243fa5c0 (patch) | |
tree | 1fcabfdb0c8d844ac51f802a0d8a9a83726f5fcb | |
parent | ae25cdb6881cef632d42100a5237850cae1c3a77 (diff) | |
download | jquery-ui-556b2710f0f09b76909b92c751edc3f4243fa5c0.tar.gz jquery-ui-556b2710f0f09b76909b92c751edc3f4243fa5c0.zip |
Widget: Support mixins
Fixes #12601
Closes gh-1554
-rw-r--r-- | tests/unit/widget/core.js | 79 | ||||
-rw-r--r-- | ui/widget.js | 4 |
2 files changed, 83 insertions, 0 deletions
diff --git a/tests/unit/widget/core.js b/tests/unit/widget/core.js index 74967e683..c1fb44f3c 100644 --- a/tests/unit/widget/core.js +++ b/tests/unit/widget/core.js @@ -476,6 +476,85 @@ test( "._superApply()", function() { delete $.fn.testWidget2; }); +test( "mixins", function() { + expect( 5 ); + + var mixin1 = { + foo: function() { + equal( method, "foo", "Methods from first mixin are copied over" ); + } + }; + var mixin2 = { + bar: function() { + equal( method, "bar", "Methods from second mixin are copied over" ); + } + }; + var prototype = { + baz: function() { + equal( method, "baz", "Methods from protoype are copied over" ); + } + }; + var existingBar = mixin2.bar; + var method; + + $.widget( "ui.testWidget", [ mixin1, mixin2, prototype ] ); + method = "foo"; + $.ui.testWidget.prototype.foo(); + method = "bar"; + $.ui.testWidget.prototype.bar(); + method = "baz"; + $.ui.testWidget.prototype.baz(); + + mixin1.foo = function() { + ok( false, "Changes to a mixin don't change the prototype" ); + }; + method = "foo"; + $.ui.testWidget.prototype.foo(); + + $.ui.testWidget.prototype.bar = function() {}; + strictEqual( mixin2.bar, existingBar, "Changes to a prototype don't change the mixin" ); +}); + +test( "mixins with inheritance", function() { + expect( 4 ); + + var mixin1 = { + foo: function() { + equal( method, "foo", "Methods from first mixin are copied over" ); + } + }; + var mixin2 = { + bar: function() { + equal( method, "bar", "Methods from second mixin are copied over" ); + } + }; + var parentPrototype = { + baz: function() { + equal( method, "baz", "Methods from parent protoype are copied over" ); + } + }; + var childPrototype = { + qux: function() { + equal( method, "qux", "Methods from child protoype are copied over" ); + } + }; + var method; + + $.widget( "ui.testWidget", [ mixin1, parentPrototype ] ); + $.widget( "ui.testWidget2", $.ui.testWidget, [ mixin2, childPrototype ] ); + method = "foo"; + $.ui.testWidget2.prototype.foo(); + method = "bar"; + $.ui.testWidget2.prototype.bar(); + method = "baz"; + $.ui.testWidget2.prototype.baz(); + method = "qux"; + $.ui.testWidget2.prototype.qux(); + + delete $.ui.testWidget2; + delete $.fn.testWidget2; +}); + test( ".option() - getter", function() { expect( 6 ); $.widget( "ui.testWidget", { diff --git a/ui/widget.js b/ui/widget.js index 2bac3d8d2..ae72c709b 100644 --- a/ui/widget.js +++ b/ui/widget.js @@ -62,6 +62,10 @@ $.widget = function( name, base, prototype ) { base = $.Widget; } + if ( $.isArray( prototype ) ) { + prototype = $.extend.apply( null, [ {} ].concat( prototype ) ); + } + // create selector for plugin $.expr[ ":" ][ fullName.toLowerCase() ] = function( elem ) { return !!$.data( elem, fullName ); |