Consuming Hypermedia - Declarative UI
I’ve been going on about hypermedia for a long time now. I’ve touched both client and server side in terms of processing resource representations. There is however a big missing piece in how developers should build user interfaces. Personally I have been hooked on the idea of Web Components and I’ve had some success experimenting with using a declarative way for defining User Interface building blocks.
Declarative views for resource representations
Assuming the use of RDF (Resource Description Framework), the user interface can be defined by creating a template
for given RDF class or data type. A few examples can include: a dedicated custom element for displaying a person,
a lightbox element for images typed as schema:ImageObject
or custom datepicker for xsd:date
.
My idea for such syntax is to extend the <template>
tag so that whenever it is added to DOM, it somehow registers itself
for use in specific case. Because the template tag itself is quite dumb I would take advantage of Polymer data
binding features and Polymer.templatizer
so that developers can define dynamic templates.
At the top level, I envision a generic <object-view>
element. It would inspect the given resource and choose among the
available specialized or generic templates (see further down).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|
By setting the objectView.resource
property the element would then look for template dedicated to the ex:Person
class
(example ):
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
See the <object-view>
used again for person.avatar
? This way it would be possible to create composable user interface
with specialized building blocks. Here’s how a template for schema:ImageObject
can be rendered as a kind of a lightbox
(example ):
1 2 3 4 5 6 7 |
|
A simple proof of concept is available on Plunker
Template selection API
Further extensions could introduce new ways for selecting a template based on the resource content, based on the predicate
(for example to use different template for property ex:husband
vs property ex:wife
) or to be able to create templates
not only for resources but also for literals (for example to select templates based on language tag?)
Common elements for Hydra Core types
Building on top of the API above it would be possible to created predefined common elements for Hydra Core resources
returned by heracles. The most obvious idea is to build a reusable template for a hydra:Collection
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
See how again the <object-view>
is used to delegate the decision on what template to render.
Obviously real life Hydra templates will need to be much more sophisticated. Template for hydra:PartialCollectionView
would definitely want to hide unnecessary link. Template for hydra:Collection
would likely display a different view for
collection member from the view displaying the same object outside a collection. This could be a feature of the <object-view>
element though.
Distributing templates
Finally one wouldn’t want to declare these templates every time in an application. I imagine that a package containing reusable Hydra Core elements would be simply wired up by a single element:
1 2 3 |
|
Similar element could be offered by data publishers somewhat satisfying the REST’s code-on-demand constraint.
It is unclear however how it would be possible to customize behaviour of those templates/elements. Overriding the selected template could be an easy way out though…