Skip to content
This repository was archived by the owner on Aug 15, 2022. It is now read-only.

Commit 87bf7d2

Browse files
committed
Code clean up, move user fetch out of regenerate
This moves the user fetching out of generateTable, as it never really belonged there. It also cleans up some of the code and adds more comments.
1 parent 666d2b7 commit 87bf7d2

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

assets/js/rest-example-jquery.js

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ jQuery( function( $ ) {
3434
var generateTable = function( posts, users ) {
3535
var i18n = screen_data.i18n;
3636

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-
4137
// Create the table and headers.
4238
var table = $( '<table/>', { 'id': 'post-table' } );
4339
var headerRow = $( '<tr/>' ).appendTo( table );
@@ -123,7 +119,7 @@ jQuery( function( $ ) {
123119
// Set default values while we fetch.
124120
unfetchedUsers.filter( function( id ) {
125121
users[ id ] = {
126-
name: '?',
122+
name: screen_data.i18n.loading,
127123
link: '#'
128124
};
129125
} );
@@ -134,13 +130,18 @@ jQuery( function( $ ) {
134130
'per_page=' + unfetchedUsers.length;
135131
$.ajax( {
136132
url: screen_data.api_root + apiString,
133+
cache: false,
134+
135+
// On success, assign user to list.
137136
success: function( data ) {
138137
data.filter( function( user ) {
139138
users[ user.id ] = user;
140139
} );
141140

142141
regenerateTable();
143142
},
143+
144+
// On error, in a real app, display an error message to user.
144145
error: function( req ) {
145146
console.error( 'error on users request' );
146147
console.error( req );
@@ -158,10 +159,13 @@ jQuery( function( $ ) {
158159
req.setRequestHeader( 'X-WP-Nonce', screen_data.api_nonce );
159160
},
160161
data: props,
162+
163+
// On success, update just this post.
161164
success: function( data ) {
162-
// Update the global state
163165
regeneratePost( data );
164166
},
167+
168+
// On error, in a real app, display an error message to user.
165169
error: function( req ) {
166170
console.error( 'error on post update' );
167171
console.error( req );
@@ -194,24 +198,33 @@ jQuery( function( $ ) {
194198
beforeSend: function( req ) {
195199
req.setRequestHeader( 'X-WP-Nonce', screen_data.api_nonce );
196200
},
201+
cache: false,
202+
203+
// On success, replace the global post list and update the entire table.
197204
success: function( data ) {
198205
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+
199211
regenerateTable();
200-
// TODO: Fetch users here instead of in regenerateTable()
201212
},
213+
214+
// On error, in a real app, display an error message to user.
202215
error: function( req ) {
203216
console.error( 'error on posts request' );
204217
console.error( req );
205218
},
219+
220+
// Regardless of success/error, clear our (loading) message.
206221
complete: function() {
207222
updateMessage( '' );
208-
},
209-
cache: false
223+
}
210224
} );
211225
};
212226

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.
215228
$( '#search-box' ).on( 'input', function( evt ) {
216229
clearTimeout();
217230

0 commit comments

Comments
 (0)