JGonera has uploaded a new change for review.
https://gerrit.wikimedia.org/r/59784
Change subject: Add _super() to View
......................................................................
Add _super() to View
This adds a possibility of calling parent's function from within child
function.
Change-Id: I9ce59f9af278f88c07f77629691ab14921b00dcd
---
M javascripts/common/mf-oop.js
M tests/javascripts/common/test_mf-oop.js
2 files changed, 38 insertions(+), 5 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MobileFrontend
refs/changes/84/59784/1
diff --git a/javascripts/common/mf-oop.js b/javascripts/common/mf-oop.js
index c98b623..098992f 100644
--- a/javascripts/common/mf-oop.js
+++ b/javascripts/common/mf-oop.js
@@ -1,7 +1,7 @@
-( function( M, $ ) {
+( function( M ) {
function extend( prototype ) {
- var Parent = this;
+ var Parent = this, key;
function Child() {
return Parent.apply( this, arguments );
}
@@ -9,7 +9,32 @@
Surrogate.prototype = Parent.prototype;
Child.prototype = new Surrogate();
- $.extend( Child.prototype, prototype );
+ // http://ejohn.org/blog/simple-javascript-inheritance
+ // Copy the properties over onto the new prototype
+ for ( key in prototype ) {
+ // Check if we're overwriting an existing function
+ if ( typeof prototype[key] === 'function' && typeof
Parent.prototype[key] === 'function' ) {
+ Child.prototype[key] = ( function( key, fn ) {
+ return function() {
+ var tmp = this._super, ret;
+
+ // Add a new ._super() method
that is the same method
+ // but on the super-class
+ this._super =
Parent.prototype[key];
+
+ // The method only need to be
bound temporarily, so we
+ // remove it when we're done
executing
+ ret = fn.apply(this, arguments);
+ this._super = tmp;
+
+ return ret;
+ };
+ } )( key, prototype[key] );
+ } else {
+ Child.prototype[key] = prototype[key];
+ }
+ }
+
Child.extend = extend;
return Child;
}
@@ -18,4 +43,4 @@
extend: extend
} );
-}( mw.mobileFrontend, jQuery ) );
+}( mw.mobileFrontend ) );
diff --git a/tests/javascripts/common/test_mf-oop.js
b/tests/javascripts/common/test_mf-oop.js
index 74ea1c8..71367ac 100644
--- a/tests/javascripts/common/test_mf-oop.js
+++ b/tests/javascripts/common/test_mf-oop.js
@@ -4,7 +4,7 @@
QUnit.module( 'MobileFrontend oop' );
-QUnit.test( '#extend', 4, function() {
+QUnit.test( '#extend', 5, function() {
var Child, child;
function Parent() {}
@@ -17,6 +17,10 @@
return 'override';
};
+ Parent.prototype.callSuper = function() {
+ return 'super';
+ };
+
Parent.extend = oop.extend;
Child = Parent.extend( {
@@ -25,6 +29,9 @@
},
child: function() {
return 'child';
+ },
+ callSuper: function() {
+ return this._super() + ' duper';
}
} );
@@ -32,6 +39,7 @@
strictEqual( child.parent(), 'parent', 'inherit parent properties' );
strictEqual( child.override(), 'overriden', 'override parent
properties' );
strictEqual( child.child(), 'child', 'add new properties' );
+ strictEqual( child.callSuper(), 'super duper', "call parent's
functions" );
strictEqual( Child.extend, oop.extend, 'make Child extendeable' );
} );
--
To view, visit https://gerrit.wikimedia.org/r/59784
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I9ce59f9af278f88c07f77629691ab14921b00dcd
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/MobileFrontend
Gerrit-Branch: master
Gerrit-Owner: JGonera <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits