r107102 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r107101‎ | r107102 | r107103 >
Date:19:57, 22 December 2011
Author:rmoen
Status:ok (Comments)
Tags:
Comment:
reworked last commit to filter prior to load method, fix ie7 cache problem with loadItem, follow up r107093
Modified paths:
  • /trunk/extensions/MarkAsHelpful/modules/ext.markAsHelpful/ext.markAsHelpful.js (modified) (history)

Diff [purge]

Index: trunk/extensions/MarkAsHelpful/modules/ext.markAsHelpful/ext.markAsHelpful.js
@@ -11,10 +11,14 @@
1212 selector: '[class^=markashelpful-]', //only selector for now
1313
1414 init: function() {
15 - var $mahWrap = $( '<div />' ).attr( 'class', 'mw-mah-wrapper' );
16 -
 15+ var props;
1716 $( mah.selector ).each ( function (i, e) {
18 - mah.loadItem( $( this ) );
 17+ props = mah.getItemProperties ( $(this) );
 18+ //be sure to only load once per item id
 19+ if( $.inArray( props.item, mah.ids ) === -1 ) {
 20+ mah.ids.push(props.item);
 21+ mah.loadItem( $( this ) );
 22+ }
1923 });
2024 },
2125
@@ -34,41 +38,35 @@
3539 * Load the current state of the MarkAsHelpful item
3640 */
3741 loadItem: function( $item ) {
38 - var props = mah.getItemProperties( $item );
 42+ var props = mah.getItemProperties( $item ),
 43+ request = {
 44+ 'action': 'getmarkashelpfulitem',
 45+ 'item': props.item,
 46+ 'type': props.type,
 47+ 'format': 'json'
 48+ };
3949
40 - //only inject once per item id to prevent loading mutiple of the same hook
41 - if( $.inArray( props.item, mah.ids ) === -1 ) {
42 - mah.ids.push(props.item);
43 -
44 - var request = {
45 - 'action': 'getmarkashelpfulitem',
46 - 'item': props.item,
47 - 'type': props.type,
48 - 'format': 'json'
 50+ $.ajax({
 51+ type: 'get',
 52+ url: mw.util.wikiScript('api') + '?' + Math.random(Date.now), // added randomness to prevent ie7 cache
 53+ data: request,
 54+ success: function( data ) {
4955
50 - };
51 - $.ajax({
52 - type: 'get',
53 - url: mw.util.wikiScript('api'),
54 - data: request,
55 - success: function( data ) {
56 -
57 - if ( data && data.getmarkashelpfulitem.result == 'success' &&
58 - data.getmarkashelpfulitem.formatted
59 - ) {
60 -
61 - var $content = $( data.getmarkashelpfulitem.formatted );
62 - $item.html( $content );
63 - } else {
64 - // Failure, do nothing to the item for now
65 - }
66 - },
67 - error: function ( data ) {
 56+ if ( data && data.getmarkashelpfulitem.result == 'success' &&
 57+ data.getmarkashelpfulitem.formatted
 58+ ) {
 59+ var $content = $( data.getmarkashelpfulitem.formatted );
 60+ $item.html( $content );
 61+ } else {
6862 // Failure, do nothing to the item for now
69 - },
70 - dataType: 'json'
71 - });
72 - }
 63+ }
 64+ },
 65+ error: function ( data ) {
 66+ // Failure, do nothing to the item for now
 67+ },
 68+ dataType: 'json'
 69+ });
 70+
7371 },
7472 /*
7573 * API call to mark or unmark an item as helpful.
@@ -78,8 +76,7 @@
7977 clientData = $.client.profile(),
8078 request;
8179 props.mahaction = action;
82 -
83 - apiRequest = $.extend( {
 80+ request = $.extend( {
8481 'action': 'markashelpful',
8582 'page': mw.config.get( 'wgPageName' ),
8683 'useragent': clientData.name + '/' + clientData.versionNumber,
@@ -91,9 +88,8 @@
9289 $.ajax( {
9390 type: 'post',
9491 url: mw.util.wikiScript( 'api' ),
95 - data: apiRequest,
 92+ data: request,
9693 success: function () {
97 - mah.ids.removeItemByValue(props.item);
9894 mah.loadItem( $item );
9995 },
10096 dataType: 'json'
@@ -119,21 +115,6 @@
120116 var $item = $( this ).parent().parent();
121117 mah.markItem( $item, 'unmark' );
122118 } );
123 -
124 - /*
125 - * function removeItemByValue
126 - * removes an item from array by value
127 - */
128 - Array.prototype.removeItemByValue= function(){
129 - var what, a= arguments, L= a.length, ax;
130 - while(L && this.length){
131 - what= a[--L];
132 - while((ax= this.indexOf(what))!= -1){
133 - this.splice(ax, 1);
134 - }
135 - }
136 - return this;
137 - };
138119
139120 // Initialize MarkAsHelpful
140121 $( mah.init );

Follow-up revisions

RevisionCommit summaryAuthorDate
r107139Followup r107102 - consistent whitespacenikerabbit08:41, 23 December 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r107093added method to remove item from array by value to solve bug with reloading i...rmoen19:16, 22 December 2011

Comments

#Comment by Catrope (talk | contribs)   21:23, 22 December 2011
+				url: mw.util.wikiScript('api') + '?' + Math.random(Date.now), // added randomness to prevent ie7 cache

You can use 'cache': false, to have jQuery do this for you.

#Comment by Robmoen (talk | contribs)   23:18, 22 December 2011

Awesome, I will revise for next sprint.

Status & tagging log