JGonera has uploaded a new change for review.
https://gerrit.wikimedia.org/r/90685
Change subject: Make View's defaults inheritable and extendable
......................................................................
Make View's defaults inheritable and extendable
Now when children of View define the `defaults` property, properties of
`defaults` of the parent are preserved if the child doesn't override
them.
Previous behaviour led to missing close button message in most overlays,
which suggests that extending defaults in this new way may be more
intuitive, if slightly more magical.
Change-Id: Id6bb5a89dea33200ea9cf35535644bde54d8820e
---
M javascripts/common/Class.js
M javascripts/common/CtaDrawer.js
M javascripts/common/View.js
M javascripts/modules/editor/EditorOverlay.js
M javascripts/modules/mediaViewer.js
M javascripts/modules/talk/TalkSectionOverlay.js
M javascripts/modules/tutorials/PageActionOverlay.js
M tests/javascripts/common/test_Class.js
M tests/javascripts/common/test_View.js
9 files changed, 29 insertions(+), 8 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MobileFrontend
refs/changes/85/90685/1
diff --git a/javascripts/common/Class.js b/javascripts/common/Class.js
index cc5e604..27adfa5 100644
--- a/javascripts/common/Class.js
+++ b/javascripts/common/Class.js
@@ -9,6 +9,7 @@
function Surrogate() {}
Surrogate.prototype = Parent.prototype;
Child.prototype = new Surrogate();
+ Child.prototype._parent = Parent.prototype;
// http://ejohn.org/blog/simple-javascript-inheritance
// Copy the properties over onto the new prototype
diff --git a/javascripts/common/CtaDrawer.js b/javascripts/common/CtaDrawer.js
index 0201dd6..103fc6c 100644
--- a/javascripts/common/CtaDrawer.js
+++ b/javascripts/common/CtaDrawer.js
@@ -8,8 +8,7 @@
CtaDrawer = Drawer.extend( {
defaults: {
loginCaption: mw.msg(
'mobile-frontend-watchlist-cta-button-login' ),
- signupCaption: mw.msg(
'mobile-frontend-watchlist-cta-button-signup' ),
- cancelMessage: mw.msg( 'mobile-frontend-drawer-cancel' )
+ signupCaption: mw.msg(
'mobile-frontend-watchlist-cta-button-signup' )
},
template: M.template.get( 'ctaDrawer' ),
diff --git a/javascripts/common/View.js b/javascripts/common/View.js
index 4b21240..24c071e 100644
--- a/javascripts/common/View.js
+++ b/javascripts/common/View.js
@@ -57,6 +57,7 @@
*/
initialize: function( options ) {
this._super();
+ this.defaults = $.extend( {}, this._parent.defaults,
this.defaults );
options = $.extend( {}, this.defaults, options );
if ( options.el ) {
this.$el = $( options.el );
diff --git a/javascripts/modules/editor/EditorOverlay.js
b/javascripts/modules/editor/EditorOverlay.js
index 2ebe906..b4695f1 100644
--- a/javascripts/modules/editor/EditorOverlay.js
+++ b/javascripts/modules/editor/EditorOverlay.js
@@ -14,7 +14,6 @@
EditorOverlay = Overlay.extend( {
defaults: {
- closeMsg: mw.msg( 'mobile-frontend-overlay-escape' ),
continueMsg: mw.msg( 'mobile-frontend-editor-continue'
),
saveMsg: mw.msg( 'mobile-frontend-editor-save' ),
cancelMsg: mw.msg( 'mobile-frontend-editor-cancel' ),
diff --git a/javascripts/modules/mediaViewer.js
b/javascripts/modules/mediaViewer.js
index 2c34b1b..ddf31ac 100644
--- a/javascripts/modules/mediaViewer.js
+++ b/javascripts/modules/mediaViewer.js
@@ -47,7 +47,6 @@
closeOnBack: true,
defaults: {
- closeMsg: mw.msg( 'mobile-frontend-overlay-escape' ),
detailsMsg: mw.msg( 'mobile-frontend-media-details' )
},
diff --git a/javascripts/modules/talk/TalkSectionOverlay.js
b/javascripts/modules/talk/TalkSectionOverlay.js
index f7fdefd..c00572f 100644
--- a/javascripts/modules/talk/TalkSectionOverlay.js
+++ b/javascripts/modules/talk/TalkSectionOverlay.js
@@ -7,7 +7,6 @@
TalkSectionOverlay = Overlay.extend( {
template: M.template.get( 'talkSection' ),
defaults: {
- closeMsg: mw.msg( 'mobile-frontend-overlay-escape' ),
reply: mw.msg( 'mobile-frontend-talk-reply' ),
confirmMsg: mw.msg( 'mobile-frontend-editor-save' ),
licenseMsg: mw.msg( 'mobile-frontend-editor-license' ),
diff --git a/javascripts/modules/tutorials/PageActionOverlay.js
b/javascripts/modules/tutorials/PageActionOverlay.js
index a027cbb..be9f90f 100644
--- a/javascripts/modules/tutorials/PageActionOverlay.js
+++ b/javascripts/modules/tutorials/PageActionOverlay.js
@@ -17,7 +17,6 @@
defaults: {
className: 'slide active photo-upload',
summary: mw.msg(
'mobile-frontend-lead-image-tutorial-summary' ),
- cancelMsg: mw.msg( 'cancel' )
},
postRender: function( options ) {
this._super( options );
diff --git a/tests/javascripts/common/test_Class.js
b/tests/javascripts/common/test_Class.js
index a7ea225..2b739b4 100644
--- a/tests/javascripts/common/test_Class.js
+++ b/tests/javascripts/common/test_Class.js
@@ -4,10 +4,11 @@
QUnit.module( 'MobileFrontend Class' );
- QUnit.test( '.extend', 5, function( assert ) {
+ QUnit.test( '.extend', 6, function( assert ) {
var Parent, Child, child;
Parent = Class.extend( {
+ prop: 'parent',
parent: function() {
return 'parent';
},
@@ -20,6 +21,7 @@
} );
Child = Parent.extend( {
+ prop: 'child',
override: function() {
return 'overriden';
},
@@ -33,9 +35,10 @@
child = new Child();
assert.strictEqual( child.parent(), 'parent', 'inherit parent
properties' );
- assert.strictEqual( child.override(), 'overriden', 'override
parent properties' );
+ assert.strictEqual( child.override(), 'overriden', 'override
parent functions' );
assert.strictEqual( child.child(), 'child', 'add new
properties' );
assert.strictEqual( child.callSuper(), 'super duper', "call
parent's functions" );
+ assert.strictEqual( child._parent.prop, 'parent', "access
parent's prototype through _parent" );
assert.strictEqual( Child.extend, Class.extend, 'make Child
extendeable' );
} );
diff --git a/tests/javascripts/common/test_View.js
b/tests/javascripts/common/test_View.js
index 95cfa7b..72106e0 100644
--- a/tests/javascripts/common/test_View.js
+++ b/tests/javascripts/common/test_View.js
@@ -65,6 +65,27 @@
strictEqual( view.content(), 'Some content', 'fill template with data
from options' );
} );
+QUnit.test( 'View.extend, extending defaults', 1, function( assert ) {
+ var ParentView, ChildView, view;
+
+ ParentView = View.extend( {
+ defaults: {
+ a: 1,
+ b: 2
+ }
+ } );
+
+ ChildView = ParentView.extend( {
+ defaults: {
+ b: 3,
+ c: 4
+ }
+ } );
+
+ view = new ChildView( { c: 5 } );
+ assert.deepEqual( view.options, { a:1, b: 3, c: 5 } );
+} );
+
QUnit.test( 'View#preRender', 1, function() {
var ChildView, view;
ChildView = View.extend( {
--
To view, visit https://gerrit.wikimedia.org/r/90685
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Id6bb5a89dea33200ea9cf35535644bde54d8820e
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