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

Commit aa7af80

Browse files
committed
Add sticky toggles to post list.
This adds sticky toggle checkboxes to the post list, allowing the user to toggle the sticky state of each post as a demonstration of sending updates via the REST API.
1 parent f5e6ecc commit aa7af80

File tree

2 files changed

+57
-14
lines changed

2 files changed

+57
-14
lines changed

assets/js/rest-example-jquery.js

Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,18 @@ jQuery( function( $ ) {
33

44
var g_users = {};
55
var g_posts = [];
6+
var g_searchTimeout = null;
67

78
// Updates the search box message.
89
var updateMessage = function( text ) {
910
$( '#search-message' ).text( text );
10-
}
11+
};
1112

1213
// Regenerates the table based on the current globals.
1314
var regenerateTable = function() {
1415
var newTable = generateTable( g_posts, g_users );
1516
$( '#post-table' ).replaceWith( newTable );
16-
}
17+
};
1718

1819
// Creates a new table to display post results.
1920
var generateTable = function( posts, users ) {
@@ -28,6 +29,7 @@ jQuery( function( $ ) {
2829
var headerRow = $( '<tr/>' ).appendTo( table );
2930
$( '<th/>', { text: i18n.post } ).appendTo( headerRow );
3031
$( '<th/>', { text: i18n.author } ).appendTo( headerRow );
32+
$( '<th/>', { text: i18n.sticky } ).appendTo( headerRow );
3133

3234
// For each post, create a row.
3335
posts.filter( function( post ) {
@@ -47,10 +49,21 @@ jQuery( function( $ ) {
4749
'href': author.link,
4850
text: author.name
4951
} ).appendTo( authorCell );
52+
53+
// Sticky
54+
var stickyCell = $( '<td/>' ).appendTo( row );
55+
var stickyInput = $( '<input/>', {
56+
'type': 'checkbox',
57+
} ).appendTo( stickyCell );
58+
stickyInput.prop( 'checked', post.sticky );
59+
60+
stickyInput.on( 'change', function( evt ) {
61+
toggleSticky( post, stickyInput );
62+
} );
5063
} );
5164

5265
return table;
53-
}
66+
};
5467

5568
// Returns the ids which aren't in the users list.
5669
var filterUnfetchedUsers = function( ids, users ) {
@@ -65,7 +78,7 @@ jQuery( function( $ ) {
6578
}
6679

6780
return unfetched;
68-
}
81+
};
6982

7083
// Fetches the users which aren't in the users list, then adds them to it.
7184
var fetchUsers = function( ids, users ) {
@@ -81,7 +94,7 @@ jQuery( function( $ ) {
8194
users[ id ] = {
8295
name: '?',
8396
link: '#'
84-
}
97+
};
8598
} );
8699

87100
// Fetch those unfetched users.
@@ -103,17 +116,46 @@ jQuery( function( $ ) {
103116
},
104117
cache: false
105118
} );
106-
}
119+
};
107120

121+
var toggleSticky = function( post, checkbox ) {
122+
var sticky = ! post.sticky;
123+
checkbox.prop( 'disabled', true );
108124

109-
var timeout = null;
125+
$.ajax( {
126+
url: screen_data.api_root + 'wp/v2/posts/' + post.id,
127+
method: 'POST',
128+
beforeSend: function( req ) {
129+
req.setRequestHeader( 'X-WP-Nonce', screen_data.api_nonce );
130+
},
131+
data: {
132+
'sticky': sticky
133+
},
134+
success: function( data ) {
135+
// Update the global state
136+
g_posts[ data.id ] = data;
137+
},
138+
error: function( req ) {
139+
console.error( 'error on sticky update' );
140+
console.error( req );
141+
},
142+
complete: function() {
143+
// Either way, make sure the checkbox matches.
144+
var sticky = g_posts[ post.id ].sticky;
145+
if ( sticky !== checkbox.prop( 'checked' ) ) {
146+
checkbox.prop( 'checked', g_posts[ post.id ].sticky );
147+
}
148+
checkbox.prop( 'disabled', false );
149+
}
150+
} );
151+
};
110152

111153
var clearTimeout = function() {
112-
if ( timeout ) {
113-
window.clearTimeout( timeout );
114-
timeout = null;
154+
if ( g_searchTimeout ) {
155+
window.clearTimeout( g_searchTimeout );
156+
g_searchTimeout = null;
115157
}
116-
}
158+
};
117159

118160
var searchPosts = function() {
119161
updateMessage( screen_data.i18n.loading );
@@ -137,16 +179,16 @@ jQuery( function( $ ) {
137179
},
138180
cache: false
139181
} );
140-
}
182+
};
141183

142184
// Automatically search for what's in the text box
143185
// if it sits for long enough.
144186
$( '#search-box' ).on( 'input', function( evt ) {
145187
clearTimeout();
146188

147-
timeout = window.setTimeout( function() {
189+
g_searchTimeout = window.setTimeout( function() {
148190
searchPosts();
149-
timeout = null;
191+
g_searchTimeout = null;
150192
}, 500 );
151193
} );
152194

wp-rest-api-plugin-jquery-example.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public function output_page() {
6464
'i18n' => array(
6565
'post' => __( 'Post', $text_domain ),
6666
'author' => __( 'Author', $text_domain ),
67+
'sticky' => __( 'Sticky', $text_domain ),
6768
'loading' => __( '(loading)', $text_domain ),
6869
)
6970
) );

0 commit comments

Comments
 (0)