📖Guide: HTML & JourneyApps iFrame Client
This article provides best practices and guides on how to import interactive custom HTML iFrames into the JourneyApps runtime using the JourneyApps iFrame Client and the html
UI component.
The JourneyApps iFrame Client library is used inside an HTML file and allows your JourneyApps application and the HTML file to communicate with one another. This communication is achieved through combining the following methods in the HTML file and the application's JS/TS.
Example Overview
In the two examples below, we’ll display a line chart using Chart.js containing a count of the US population by year, which is stored in your app's DB. Each of these DB objects will have count
, nation
, and year
fields. We’ll then render the count by year for the United States in an html
component.
Data Model & Sample Data
Follow the steps below to set up the data model and fetch the population data which will seed the JourneyApps DB with sample data.
Update your Data Model and include the following model:
We'll use the Data USA API to get the population count by year and nation, then save this to DB
objects in JourneyApps. Each population object will contain a nation, year, and population count. Start by creating a function called getData
in our main.js
:
Call the getData function in the init()
function of your main.js
:
Basic Example
In this basic example, we’ll create a single HTML file and upload this file into OXIDE for further editing.
Create a new HTML file e.g. chart.html using the following template:
Notice that we added two script elements, one includes the JourneyApps iFrame Client and the other includes ChartJS. This is the easiest way to reference the iFrame client in your HTML file.
Upload this file to OXIDE by navigating to the Assets workspace and dragging/dropping the chart.html file created in the previous step into the Assets/html directory as shown on the left-hand side of the Assets panel.
Next, the uploaded file in OXIDE and update the body
element of the file to include the following:
This update to the body
includes the following changes:
We added a few style attributes for our chart containers
Added an
h3
element that displays "Loading Chart", but this gets removed when the JourneyApps Container returns a successful responseWe defined two
div
elements. One will act as a container, and another will hold our canvas element. This is where the ChartJS chart will be rendered when the JourneyApps Runtime posts the population data to the iFrame.We create a new script element that initializes the JourneyApps iFrame Client (i.e.
JourneyIFrameClient
)The script also includes an event listener that fires when the HTML has completely loaded (DOMContentLoaded). This will post a message to the JourneyApps runtime letting it know it’s ready and can start accepting data from the JourneyApps runtime
This is done by the following:
client.post("mounted", true)
, which sends a message from the HTML file to the app JS runtime.The app JS listens for messages on “mounted” with a
on(“mounted”)
method definition and returns true once it receives a message on this ‘channel’
Finally, the script includes a JourneyIFrameClient event listener in the HTML file. This tells
JourneyIFrameClient
to wait for the JourneyApps Runtime to post data to the iFrame. In this case, it will post to the ChartJS configuration object. This is done with theclient.on("render")
definition.
Now that we have an event listener waiting to receive the ChartJS configuration, we will update the main
view’s JS in OXIDE to post data to the JourneyApps iFrame Client. Let’s start by adding the chart config above the init()
function as a global view variable:
Next, we will update the init()
function to include the following:
The update to the init()
function includes the following changes:
First, we add an
on
event that listens for the mounted event from the iFrame. Without the event listener, it is not possible to post to the JourneyApps iFrame Client before it has completed theDOMContentLoaded
event. In this case, the JourneyApps Runtime will not recognize the iFrame and will fail to post.Once the mounted event is invoked we execute the
updateConfig
function, which queries the database and updates the config. We’ll add this in the next step.Once that function has completed we post the
chartConfig
object to the render event that is in the iFrame.We then return success to let the iFrame know that the job is completed, which then removes the "Loading Chart"
h3
element.
Next, add the updateConfig
function in your main
view below the init()
function. This function is responsible for retrieving the population data from the DB
and updating the chartConfig
with the correct labels and data to display in the chart:
Finally, we will add the html
UI component to our main
view XML to display the iFrame:
Notice that we added an id
attribute with a value of frame
. This can be used when you have multiple HTML iFrames and need to target specific post
or on
events. We also reference this id
when targeting the component in the runtime using component.html({ id: "frame" })
. Also see this component methods section.
Deploy your app and test the result on your device. The result should look like this:
Tip: To customize the chart, update the chartConfig
based on options available on chart.js.
Advanced Example
In this example, similar to the Basic Example above, we’ll be creating the HTML file locally and will then upload it to a JourneyApps application. However, all of the development of the iFrame will be done locally in this advanced guide. We achieve this by defining an HTML file and injecting compiled JavaScript into the file. This is especially helpful when developing more advanced iFrame clients.
Prerequisites
Yarn: You can install the latest version of yarn here.
Complete the Data Model & Sample Data section of this guide to configure and populate the data used in this example.
Setup
Create a new directory on your machine, initialize a Node.js application and create the basic file/folder structure for the project.
Install the journey-iframe-client using yarn
yarn add journey-iframe-client
Create the following file/folder structure
In this example, we are not going to write the JavaScript in our HTML as we did in the basic example. Instead, we are going to write our JS in the index.js
file and use webpack
to compile our JS and HTML files into a single file, which we will upload to OXIDE.
Update your package.json
file and add the following as a script node:
Update your package.json
with the following to your devDependencies
:
Run yarn install
to update your node modules and install all the devDependencies
Then, dd the following to your webpack.config.js
file:
You’ll also notice that we added rules to our webpack
config. These will allow us to style our iFrame using SASS and we’ve added rules for TypeScript.
Add the following to your config.yml
file:
Replace the values of the outputFileName
and htmlTitle
as you see fit. These are used by webpack
when the file is compiled and sets the output HTML filename as well as the name of the title of the HTML file.
Development
Open the src/template.html
file and add the following:
If you want to store this source code in a remote repository, please make sure you update your .gitignore
file accordingly. Here is an example:
You won’t need to push the dist
or node_modules
files to your remote repository.
Update the body
element of the src/template.html
with the following:
Update the src/index.js
with the following:
Similar to our Basic Example, we’ll add an event listener that fires when DOMContentLoaded
occurs. This will post to the JourneyApps runtime informing it that it has loaded and is ready to accept messages.
Add a few styles to your src/sass/styles.scss
:
Run the
yarn
build command in the terminal. This will run thewebpack
command and compile all the JS files, SASS files and inject them into thetemplate.html
file. The result is found in thedist/output.html
(or whatever you specified as the value for theoutputFileName
key in theconfig.yml
file).Upload the compiled HTML file to OXIDE under the Assets workspace within the /html directory.
Tip: Avoid opening the compiled HTML file on OXIDE. This file is the compiled version of your iFrame client and should not be edited in its compiled form.
Follow steps 3 - 7 in the Basic Example of this post. These steps will guide you through the process of adding the HTML component to the view XML, retrieving the sample population data from the database, and finally posting the data to the JourneyApps iFrame Client, which will render the chart and display the data. Please note that if you updated the
outputFileName
key in theconfig.yml
file, make sure you update the src attribute of the JourneyApps HTML component to reference the correct HTML file uploaded to OXIDE.Finally, test the result in your app to ensure the data is showing correctly.
The complete source code of this HTML iFrame can be found here.
Last updated