Skip to content
This repository has been archived by the owner on Sep 5, 2018. It is now read-only.

Create models before DOM ready if possible; add promises for when schema constructs #68

Merged
merged 8 commits into from
Jan 3, 2016
27 changes: 26 additions & 1 deletion client-js.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,32 @@ function json_api_client_js() {
wp_register_script( 'wp-api', $src, array( 'jquery', 'underscore', 'backbone' ), '1.0', true );
}

$settings = array( 'root' => esc_url_raw( get_rest_url() ), 'nonce' => wp_create_nonce( 'wp_rest' ) );
/**
* @var \WP_REST_Server $wp_rest_server
*/
global $wp_rest_server;
if ( empty( $wp_rest_server ) ) {
/** This filter is documented in wp-includes/rest-api.php */
$wp_rest_server_class = apply_filters( 'wp_rest_server_class', 'WP_REST_Server' );
$wp_rest_server = new $wp_rest_server_class();

/** This filter is documented in wp-includes/rest-api.php */
do_action( 'rest_api_init', $wp_rest_server );
}

$schema_request = new WP_REST_Request( 'GET', '/wp/v2' );
$schema_response = $wp_rest_server->dispatch( $schema_request );
$schema = null;
if ( ! $schema_response->is_error() ) {
$schema = $schema_response->get_data();
}

$settings = array(
'root' => esc_url_raw( get_rest_url() ),
'nonce' => wp_create_nonce( 'wp_rest' ),
'versionString' => 'wp/v2/',
'schema' => $schema,
);
wp_localize_script( 'wp-api', 'wpApiSettings', $settings );

}
Expand Down
Loading