r112440 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r112439‎ | r112440 | r112441 >
Date:15:57, 26 February 2012
Author:krinkle
Status:ok (Comments)
Tags:todo 
Comment:
[mediawiki.api.watch] Re-use watch() in unwatch().
- Fix dependency issue. In practice not an issue due to user.tokens being in the HTML output before the first mw.loader.load()
- Follows-up r107969, r107350
Modified paths:
  • /trunk/phase3/resources/Resources.php (modified) (history)
  • /trunk/phase3/resources/mediawiki.api/mediawiki.api.watch.js (modified) (history)

Diff [purge]

Index: trunk/phase3/resources/Resources.php
@@ -537,7 +537,10 @@
538538 ),
539539 'mediawiki.api.watch' => array(
540540 'scripts' => 'resources/mediawiki.api/mediawiki.api.watch.js',
541 - 'dependencies' => array( 'mediawiki.api', 'mediawiki.user' ),
 541+ 'dependencies' => array(
 542+ 'mediawiki.api',
 543+ 'user.tokens',
 544+ ),
542545 ),
543546 'mediawiki.debug' => array(
544547 'scripts' => 'resources/mediawiki/mediawiki.debug.js',
Index: trunk/phase3/resources/mediawiki.api/mediawiki.api.watch.js
@@ -12,10 +12,12 @@
1313 * @param success {Function} callback to which the watch object will be passed
1414 * watch object contains 'title' (full page name), 'watched' (boolean) and
1515 * 'message' (parsed HTML of the 'addedwatchtext' message).
 16+ * @param _unwatch {Boolean} Internally used to re-use this logic for unwatch(),
 17+ * do not use outside this module.
1618 * @param err {Function} callback if error (optional)
1719 * @return {jqXHR}
1820 */
19 - watch: function( page, success, err ) {
 21+ watch: function( page, success, err, _unwatch ) {
2022 var params, ok;
2123 params = {
2224 action: 'watch',
@@ -23,13 +25,16 @@
2426 token: mw.user.tokens.get( 'watchToken' ),
2527 uselang: mw.config.get( 'wgUserLanguage' )
2628 };
 29+ if ( _unwatch ) {
 30+ params.unwatch = 1;
 31+ }
2732 ok = function( data ) {
2833 success( data.watch );
2934 };
3035 return this.post( params, { ok: ok, err: err } );
3136 },
3237 /**
33 - * Convinience method for 'action=watch&unwatch='.
 38+ * Convinience method for 'action=watch&unwatch=1'.
3439 *
3540 * @param page {String|mw.Title} Full page name or instance of mw.Title
3641 * @param success {Function} callback to which the watch object will be passed
@@ -39,18 +44,7 @@
4045 * @return {jqXHR}
4146 */
4247 unwatch: function( page, success, err ) {
43 - var params, ok;
44 - params = {
45 - action: 'watch',
46 - unwatch: 1,
47 - title: String( page ),
48 - token: mw.user.tokens.get( 'watchToken' ),
49 - uselang: mw.config.get( 'wgUserLanguage' )
50 - };
51 - ok = function( data ) {
52 - success( data.watch );
53 - };
54 - return this.post( params, { ok: ok, err: err } );
 48+ return this.watch( page, success, err, true );
5549 }
5650
5751 } );

Follow-up revisions

RevisionCommit summaryAuthorDate
r113893[mediawiki.action.watch] Move re-used logic into local function...krinkle05:54, 15 March 2012

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r107350[mediawiki.api] write mediawiki.api.watch module...krinkle00:44, 27 December 2011
r107969Fix unused parameter from r107350...krinkle00:05, 4 January 2012
r111809Add missing dependency of mediawiki.user to mediawiki.api.watch. May resolve ...werdna00:42, 18 February 2012

Comments

#Comment by Nikerabbit (talk | contribs)   06:04, 27 February 2012

Don't we usually make shared common protected method in these cases?

#Comment by Catrope (talk | contribs)   19:15, 14 March 2012

Yes, that's what should be done. Move all the code into a protected function and make both watch and unwatch wrappers around it.

Status & tagging log