Create models before DOM ready if possible; add promises for when schema constructs#68
Conversation
* Let wp.api.init() return promise which resolves when models are constructed. * Move references from global wp.api to properties of Endpoint (and Schema) models/ * Fix serialization of schema data into sessionStorage by using toJSON.
This ensures that the models are constructed before DOM ready.
…tring match globals
|
See diff without whitespace changes to reduce the size: https://github.com/WP-API/client-js/pull/68/files?w=1 |
|
I think this is happening with the localized schema data, I will dig in more. Once the client has the schema cached, I'd like to add a cookie for subsequent requests, maybe with a schema hash, so the PHP side can exclude the data if it hasn't changed. Its about 24k of data in my local testing we shouldn't need to include with each request. |
|
Tracked this down to two issues, one with session storage and one with my new code adding the mixins/helpers. both fixed in master now |
|
🙌 |
|
@westonruter started working on only localizing schema data once in this branch: https://github.com/WP-API/client-js/tree/features/only-localize-schma-once -, i set a cookie with the schema hash, if that matches a cached value in php i skip the localization, as JS already has the schema data in sessionstorage |


The changes here will ensure that the Backbone models for the WPI API will be constructed before DOM ready if the JS is enqueued via WordPress so that the schema can be exported as script data. If the schema is not available in the initial PHP response, it will fallback to looking for a schema in
sessionStorage. Otherwise, it will fetch the schema from the server.The
wp.api.init()function now returns a promise which resolves when the models are constructed and extended ontowp.api.modelsandwp.api.collections. Thiswp.api.init()function also takes anargsparam which allows you to initialize additional endpoints other than the default (which is initialized automatically). For example:Calling
wp.api.init()repeatedly for a given endpoint will only initialize the route once: the same promise will be returned for each invocation.This introduces a new
Endpointmodel which contains aSchemamodel instance. I'm not 100% confident with how these are are organized.