Part 3: Connecting frontend and backend
The Dittofi Essentials give you a "top down" introduction to essential concepts in Dittofi. In this tutorial we will join up everything that we learnt in Part 2 and Part 3.
WHAT YOU'LL LEARN
- Connecting frontend and backend (8 steps)
- Run endpoint event
- Condition event
- Set field event
- Repeating group
- Basic Dittofi data flow
In the Part 1 of the Dittofi Essentials you learnt how to build a frontend, create frontend variables and trigger workflows to run. In Part 2, you learnt how to build a backend data store and how to build an interface into that store using endpoints.
In this section, we are going to bring all the concepts together by showing you how you can connect up your frontend and backend in 8 quick and easy steps.
Head on over to the Dittofi Page builder and create a new frontend variable called "ProductsList" and set it to type Collection.
Create a workflow called "Get products data".
Add a Run endpoint event to your workflow.
The Run endpoint event allows you to trigger a specific endpoint to run whenever your workflow runs. In our case, we want to trigger our Get products data endpoint, since we want to fetch data from our Products data model.
Fill in the Run endpoints event form as below and press "Save".
Notice, the data that is returned from our Get products data endpoint is mapped into the Response As and a Response Code As fields.
The Response As field gives us an option to add a variable, or placeholder, for the collection of products returned by our endpoint. Meanwhile, the Response Code As field gives us an option to add a variable, or placeholder for the response code returned by our endpoint. Recall that a response code 200 means that the endpoint has run successfully.
Checking that an endpoint has run successfully is good practice. To do this, we begin by creating a variable within our workflow called "SuccessCode" and setting its value equal to 200.
We then add the condition event within our workflow. We use the event to compare the endpoint Response Code against our SuccessCode variable.
If the Response Code is equal to the SuccessCode we know that our endpoint ran successfully.
Add the Set Field event inside of our condition statement.
The set field event lets you set the value of a variable in your app. In our case, we want to take the data returned from our endpoint and map this data into our frontend ProductsList variable.
The final set field event configuration is shown below.
Note, because the set field event is inside our condition statement, it will only run if the endpoint returns a success code 200.
Add the following elements to the page a div block and inside the div block add an image, heading and heading (h3) elements.
Next, turn your div block into a repeating group.
A repeating group is a container that repeats for every value in a list.
In our case we bound our div block to our ProductsList variable. This means that our div block will now repeat for every row of data stored inside our products data model and returned by our endpoint to our workflow.
Since each row in the repeating group contains columns, i.e. product, category and image. We need to choose which element inside our repeating group will display which column. In our case we can set the image component to the image field, the heading to the product and label to the category.
To get the workflow to trigger when your app loads, re-open your workflow and change the trigger from "Component" to "On app mount".
Click "Build code" and then preview your app in a live environment. You should see a list of the data in your products table displayed down the screen, as below.
There are quite a few steps when linking the frontend and backend of a Dittofi app. There are various shortcuts, however, this taking it step by step will help you to maximize your understanding of the app development process.
In this section we are going to zoom out and recap what we have done so far, with a high level diagram (shown below) that explains how data flows around the system.
GIF showing how data is passed around the Dittofi system
In Parts 1 - 3 of this tutorial we have learnt
- Dittofi frontend essentials
- Dittofi backend essentials
- How to connect your Dittofi frontend to your Dittofi backend
At this point, you should feel relatively comfortable with the frontend, backed concepts and how Dittofi apps hang together. Now all you need is more practice! To help with this, we will continue on to Part 4: Dittofi App Structure, where we'll look at a full working example of how to build a Netflix style data streaming service on Dittofi together.