-
Notifications
You must be signed in to change notification settings - Fork 122
Client side rendering
Matteo Figus edited this page Mar 10, 2017
·
5 revisions
To make this happen, a components registry has to be publicly available. This is how to integrate the component:
<html>
<head></head>
<body>
<oc-component href="//my-components-registry.mydomain.com/hello-world/1.X.X"></oc-component>
<script src="//my-components-registry.mydomain.com/oc-client/client.js"></script>
</body>
</html>
- It is recommended to use protocol-less urls for rendering so that we can use https where possible for security reasons
- The
/oc-client/client.js
is a convenient shortcut for serving the browser oc-client of the same version as the registry. - The client can be before or after the components. When after, a rendering needs to happen via explicit call as described in this page: advanced client-side operations.
- The first thing is for the client to be initialised. This includes verifying the dependencies are satisfied (for instance, jQuery)
- At the end of the initialisation, the client is going to fire an event
oc:ready
- Then the client is going to scan for
<oc-component>
tags in the page and for each one of them: - It is going to make an HTTP GET to the
href
parameter of the component, sending aAccept: application/vnd.oc.unrendered+json
header (which is the standard header for getting an unrendered component). - In case of error (timeout or 5xx response) the client is going to retry 30 times (retry attempts and timeout are configurable)
- In case of success, the client is going to find what kind of template the component uses and verify dependencies are satisfied. In case of Handlebars, for instance, it is going to ensure the handlebars runtime is available and ready, if it is not it's going to download it and then go to the next step
- The client will take the compiled view url, for instance
//cdn.com/components/my-component/1.2.3/template.js
and put it in the<head>
tag, then waiting for it to be ready. If the template is already there (for example because there are multiple components of the same type and version in the page) it is going to skip step 7. When the template is ready it is exposed in thewindow.oc.components['hash-of-the-component']
namespace as a function:view(viewModel)=html
. - This is going to take the viewModel (part of the HTTP response) and use it to render the view, in order to obtain the html. When finished, it is going to place the HTML result inside the
<oc-component>
container tag. Some templates may perform additional actions. In case of a React template, for instance, the HTML may be bound to the Virtual DOM. - An
oc:rendered
event is published containing the pointer to the component, and all the information (version, name, html, etc.) - Back to 3) In order to render, in case, nested client-side components
Look at this page: advanced client-side operations