@@ -34,10 +34,6 @@ jQuery( function( $ ) {
34
34
var generateTable = function ( posts , users ) {
35
35
var i18n = screen_data . i18n ;
36
36
37
- // Get all of the authors for the posts we're showing.
38
- var authorIds = posts . map ( function ( post ) { return post . author . toString ( ) ; } ) ;
39
- fetchUsers ( authorIds , users ) ;
40
-
41
37
// Create the table and headers.
42
38
var table = $ ( '<table/>' , { 'id' : 'post-table' } ) ;
43
39
var headerRow = $ ( '<tr/>' ) . appendTo ( table ) ;
@@ -123,7 +119,7 @@ jQuery( function( $ ) {
123
119
// Set default values while we fetch.
124
120
unfetchedUsers . filter ( function ( id ) {
125
121
users [ id ] = {
126
- name : '?' ,
122
+ name : screen_data . i18n . loading ,
127
123
link : '#'
128
124
} ;
129
125
} ) ;
@@ -134,13 +130,18 @@ jQuery( function( $ ) {
134
130
'per_page=' + unfetchedUsers . length ;
135
131
$ . ajax ( {
136
132
url : screen_data . api_root + apiString ,
133
+ cache : false ,
134
+
135
+ // On success, assign user to list.
137
136
success : function ( data ) {
138
137
data . filter ( function ( user ) {
139
138
users [ user . id ] = user ;
140
139
} ) ;
141
140
142
141
regenerateTable ( ) ;
143
142
} ,
143
+
144
+ // On error, in a real app, display an error message to user.
144
145
error : function ( req ) {
145
146
console . error ( 'error on users request' ) ;
146
147
console . error ( req ) ;
@@ -158,10 +159,13 @@ jQuery( function( $ ) {
158
159
req . setRequestHeader ( 'X-WP-Nonce' , screen_data . api_nonce ) ;
159
160
} ,
160
161
data : props ,
162
+
163
+ // On success, update just this post.
161
164
success : function ( data ) {
162
- // Update the global state
163
165
regeneratePost ( data ) ;
164
166
} ,
167
+
168
+ // On error, in a real app, display an error message to user.
165
169
error : function ( req ) {
166
170
console . error ( 'error on post update' ) ;
167
171
console . error ( req ) ;
@@ -194,24 +198,33 @@ jQuery( function( $ ) {
194
198
beforeSend : function ( req ) {
195
199
req . setRequestHeader ( 'X-WP-Nonce' , screen_data . api_nonce ) ;
196
200
} ,
201
+ cache : false ,
202
+
203
+ // On success, replace the global post list and update the entire table.
197
204
success : function ( data ) {
198
205
g_posts = data ;
206
+
207
+ // Get all of the authors for the posts we're about to show.
208
+ var authorIds = g_posts . map ( function ( post ) { return post . author . toString ( ) ; } ) ;
209
+ fetchUsers ( authorIds , g_users ) ;
210
+
199
211
regenerateTable ( ) ;
200
- // TODO: Fetch users here instead of in regenerateTable()
201
212
} ,
213
+
214
+ // On error, in a real app, display an error message to user.
202
215
error : function ( req ) {
203
216
console . error ( 'error on posts request' ) ;
204
217
console . error ( req ) ;
205
218
} ,
219
+
220
+ // Regardless of success/error, clear our (loading) message.
206
221
complete : function ( ) {
207
222
updateMessage ( '' ) ;
208
- } ,
209
- cache : false
223
+ }
210
224
} ) ;
211
225
} ;
212
226
213
- // Automatically search for what's in the text box
214
- // if it sits for long enough.
227
+ // Automatically search for what's in the text box if it sits long enough.
215
228
$ ( '#search-box' ) . on ( 'input' , function ( evt ) {
216
229
clearTimeout ( ) ;
217
230
0 commit comments