From 55b529efb5827c843e95ead87157044fd7c559c4 Mon Sep 17 00:00:00 2001 From: Stanislav Ievlev Date: Tue, 22 Jan 2013 15:46:04 +0400 Subject: [PATCH 1/3] Implement private methods Store private methods in separate containter accessible via special _private call. All methods started with a two underscore symbols treated as a private. --- ui/jquery.ui.widget.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/ui/jquery.ui.widget.js b/ui/jquery.ui.widget.js index c581e4b8156..a2e8ccc8145 100644 --- a/ui/jquery.ui.widget.js +++ b/ui/jquery.ui.widget.js @@ -28,6 +28,7 @@ $.widget = function( name, base, prototype ) { // proxiedPrototype allows the provided prototype to remain unmodified // so that it can be used as a mixin for multiple widgets (#8876) proxiedPrototype = {}, + privatePrototype = {}, namespace = name.split( "." )[ 0 ]; name = name.split( "." )[ 1 ]; @@ -78,29 +79,40 @@ $.widget = function( name, base, prototype ) { proxiedPrototype[ prop ] = value; return; } - proxiedPrototype[ prop ] = (function() { + var func = (function() { var _super = function() { return base.prototype[ prop ].apply( this, arguments ); }, _superApply = function( args ) { return base.prototype[ prop ].apply( this, args ); + }, + _private = function(prop) { + return privatePrototype[ prop ].apply(this, slice.call(arguments, 1)); }; return function() { var __super = this._super, __superApply = this._superApply, + __private = this._private, returnValue; this._super = _super; this._superApply = _superApply; + this._private = _private; returnValue = value.apply( this, arguments ); this._super = __super; this._superApply = __superApply; + this._private = __private; return returnValue; }; })(); + if (prop.indexOf("__") === 0) { + privatePrototype[prop] = func; + } else { + proxiedPrototype[ prop ] = func; + } }); constructor.prototype = $.widget.extend( basePrototype, { // TODO: remove support for widgetEventPrefix From 2cced03c72850ea682e42b63c8916bb31785ca49 Mon Sep 17 00:00:00 2001 From: Stanislav Ievlev Date: Tue, 22 Jan 2013 15:50:27 +0400 Subject: [PATCH 2/3] add unit tests for private methods --- tests/unit/widget/widget_core.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/unit/widget/widget_core.js b/tests/unit/widget/widget_core.js index 8102b1f4fa8..4aed98fd435 100644 --- a/tests/unit/widget/widget_core.js +++ b/tests/unit/widget/widget_core.js @@ -375,6 +375,37 @@ test( "inheritance", function() { delete $.ui.testWidgetExtension; }); +test( "private methods", function() { + expect( 4 ); + + $.widget( "ui.testWidgetBase", { + publicBase: function() { + deepEqual("a", this._private("__private"), "private method call"); + deepEqual("b", this._protected(), "protected method call"); + }, + _protected: function() { return "a"; }, + __private: function() { return "a"; } + }); + + $.widget( "ui.testWidgetExtension", $.ui.testWidgetBase, { + publicExt: function() { + deepEqual("b", this._private("__private"), "private method call"); + deepEqual("b", this._protected(), "protected method call"); + }, + _protected: function() { return "b"; }, + __private: function() { return "b"; } + }); + + var instance = $( "
" ).testWidgetExtension(); + instance.testWidgetExtension("publicBase"); + instance.testWidgetExtension("publicExt"); + + delete $.ui.testWidgetBase; + delete $.ui.testWidgetExtension; + delete $.fn.testWidgetBase; + delete $.fn.testWidgetExtension; +}); + test( "._super()", function() { expect( 9 ); var instance; From 94399e812886f5b8bfd3e3e40dc97670b83d5a07 Mon Sep 17 00:00:00 2001 From: Stanislav Ievlev Date: Tue, 22 Jan 2013 15:50:46 +0400 Subject: [PATCH 3/3] rename '__response' to '_real_response' due to a new naming convention for private methods. --- ui/jquery.ui.autocomplete.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ui/jquery.ui.autocomplete.js b/ui/jquery.ui.autocomplete.js index af1acb02b2f..124d89254d1 100644 --- a/ui/jquery.ui.autocomplete.js +++ b/ui/jquery.ui.autocomplete.js @@ -428,7 +428,7 @@ $.widget( "ui.autocomplete", { return function( content ) { if ( index === requestIndex ) { - that.__response( content ); + that._real_response( content ); } that.pending--; @@ -438,7 +438,7 @@ $.widget( "ui.autocomplete", { }; }, - __response: function( content ) { + _real_response: function( content ) { if ( content ) { content = this._normalize( content ); } @@ -596,7 +596,7 @@ $.widget( "ui.autocomplete", $.ui.autocomplete, { } }, - __response: function( content ) { + _real_response: function( content ) { var message; this._superApply( arguments ); if ( this.options.disabled || this.cancelSearch ) {