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

Conversation

westonruter
Copy link
Member

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 onto wp.api.models and wp.api.collections. This wp.api.init() function also takes an args param which allows you to initialize additional endpoints other than the default (which is initialized automatically). For example:

wp.api.init( { apiRoot: 'http://other-site.example.com/wp-json/' } ).done( function( endpoint ) {
    // now use the models it provides on wp.api.models or via endpoint.get( 'models' )
} );

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 Endpoint model which contains a Schema model instance. I'm not 100% confident with how these are are organized.

* 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.
@westonruter
Copy link
Member Author

See diff without whitespace changes to reduce the size: https://github.com/WP-API/client-js/pull/68/files?w=1

@adamsilverstein adamsilverstein merged commit bd403af into WP-API:master Jan 3, 2016
@adamsilverstein
Copy link

seeing some models fail to initialize after this merge, needs some investigation:

before:

after:

@adamsilverstein
Copy link

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.

@adamsilverstein
Copy link

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
Copy link
Member Author

🙌

@adamsilverstein
Copy link

@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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants