0

Lightweight Generic CAP UI

 2 years ago
source link: https://blogs.sap.com/2021/08/08/lightweight-generic-cap-ui/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Technical Articles
Posted on August 8, 2021 2 minute read

Lightweight Generic CAP UI

4 Likes 128 Views 2 Comments

Disclaimer: This is just me experimenting, nothing official.

Motivation

I really like the custom Bookshop UI (based on Vue.js) because it’s minimal and fast.

vue-app.png

I was wondering if I could make parts of it generic (based on a given csn entity) without sacrificing performance. That means no client-side metadata analysis.

To achieve that, I use templates to generate HTML pages.

Implementation

I chose Handlebars as my templating mechanism.

First, I need to register two express handlers in my CAP bookshop application.

served-1.png

One of them makes sure that static files are accessible, the other one uses express-handlebars to connect the Handlebars templating mechanism to my express app. Once the /app endpoint is called, the result of the rendered index page is returned.

The rendering is based on my csn definitions and uses some helper functions which I defined here:

helpers-3.png

They help me to easily use my csn definitions in my templates.

Let’s look at the index page which needs to be rendered:

index-2.png

As you can see, there are no bookshop-specific elements.

Whenever you see {{ ... }}, it means that Handlebars will inject the given expression into the resulting HTML document. We have access to all provided helper functions and input objects.

Usually, the {{ and }} symbols are text-interpolation delimiters for Vue.js. But since Handlebars uses the same symbols, I changed the Vue.js delimiters to ${ and }.

The expression {{> list }} and {{> object }} just means that I want to include my defined partials, shown below.

{{> list }} (showing the table):

items-1.png

{{> object }} (showing the selected object):

object-2.png

As you can see, I can iterate over my elements using {{#each ...}}.

Finally, the Vue.js code to make the app dynamic:

app-1.png

Final Result

The resulting app is not as pretty as the custom-made one, but good enough as a proof of concept. I’m not a frontend developer after all.

result2-1.png

You can find the complete code in my GitHub repository.

Thanks for reading and best regards,
David


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK