Index: trunk/extensions/MarkAsHelpful/modules/ext.markAsHelpful/ext.markAsHelpful.js |
— | — | @@ -11,10 +11,14 @@ |
12 | 12 | selector: '[class^=markashelpful-]', //only selector for now |
13 | 13 | |
14 | 14 | init: function() { |
15 | | - var $mahWrap = $( '<div />' ).attr( 'class', 'mw-mah-wrapper' ); |
16 | | - |
| 15 | + var props; |
17 | 16 | $( 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 | + } |
19 | 23 | }); |
20 | 24 | }, |
21 | 25 | |
— | — | @@ -34,41 +38,35 @@ |
35 | 39 | * Load the current state of the MarkAsHelpful item |
36 | 40 | */ |
37 | 41 | 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 | + }; |
39 | 49 | |
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 ) { |
49 | 55 | |
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 { |
68 | 62 | // 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 | + |
73 | 71 | }, |
74 | 72 | /* |
75 | 73 | * API call to mark or unmark an item as helpful. |
— | — | @@ -78,8 +76,7 @@ |
79 | 77 | clientData = $.client.profile(), |
80 | 78 | request; |
81 | 79 | props.mahaction = action; |
82 | | - |
83 | | - apiRequest = $.extend( { |
| 80 | + request = $.extend( { |
84 | 81 | 'action': 'markashelpful', |
85 | 82 | 'page': mw.config.get( 'wgPageName' ), |
86 | 83 | 'useragent': clientData.name + '/' + clientData.versionNumber, |
— | — | @@ -91,9 +88,8 @@ |
92 | 89 | $.ajax( { |
93 | 90 | type: 'post', |
94 | 91 | url: mw.util.wikiScript( 'api' ), |
95 | | - data: apiRequest, |
| 92 | + data: request, |
96 | 93 | success: function () { |
97 | | - mah.ids.removeItemByValue(props.item); |
98 | 94 | mah.loadItem( $item ); |
99 | 95 | }, |
100 | 96 | dataType: 'json' |
— | — | @@ -119,21 +115,6 @@ |
120 | 116 | var $item = $( this ).parent().parent(); |
121 | 117 | mah.markItem( $item, 'unmark' ); |
122 | 118 | } ); |
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 | | - }; |
138 | 119 | |
139 | 120 | // Initialize MarkAsHelpful |
140 | 121 | $( mah.init ); |