M02 - HOL Reusable Components
M02 - HOL Reusable Components
Hands-on Lab
Table of Contents
Lab Overview..................................................................................................................................................................... 1
Scenario ............................................................................................................................................................................................ 1
Things you will learn .................................................................................................................................................................. 1
Exercise 1 – Create Canvas Application .................................................................................................................................. 2
Task 1: Create Canvas Application ....................................................................................................................................... 2
Task 2: Setup the Carousel Component ............................................................................................................................. 7
Exercise 2 – Build and Consume Component .....................................................................................................................12
Task 1: Add Controls to Component ..................................................................................................................................12
Task 2: Consume the Component .......................................................................................................................................18
Task 3: Add Auto Play ..............................................................................................................................................................22
Task 4: Add Selected Record .................................................................................................................................................24
Lab Overview
Scenario
You are building a conference management application and need to display registered attendees
and be able to cycle through them. Each conference has different information that is important to
see about the attendee. For example, virtual conferences show different data than in-person
conferences. Therefore, you want to use a template associated with each conference to display
the information.
You've decided to build a reusable component that will allow viewing of the records in a carousel
that can cycle through the records. The user will be able to manually cycle through the records or
enable an automated play cycle. The content will be displayed using a dynamic HTML template
that will be used to customize the display using an HTML text control. By building a reusable
component you believe other app makers will be able to use it for other similar scenarios.
To help ensure the control will work for multiple scenarios, you have chosen to build out the
component using a test app that uses Accounts and Contacts and displays a simple business card
for each contact.
1
Reusable Components- Lab Overview
3
Reusable Components- Lab Overview
11) Click Edit Fields, select Main Phone for Subtitle2 and select Account Name for Title2.
15) Scroll down and turn on Components. Components are currently in preview and must be
enabled prior to use in your applications.
5
Reusable Components- Lab Overview
16) To work around a current bug, also turn off Explicit Column selection
17) Click OK to the dialog telling you that you will need to close the app to finish the change.
18) Select the Name + icon tab and name the application Carousel Component.
22) You should be back to the app designer. Do not navigate away from this page.
3) Next you are going to add some custom properties. Properties define how your component will
communicate with the hosting app. You can define input properties that allow the hosting app
screen to give the component data and output properties that allow the component to give the
hosting app screen data. Think of properties as defining a contract for how your component
and app will communicate. Because your component might be used by multiple apps you
should try to not make breaking changes to your input and output properties that would
impact apps using your component.
7
Reusable Components- Lab Overview
8) Add another custom property with the following information and click Create.
• Display name: Refresh Data
9
Reusable Components- Lab Overview
• Name: RefreshData
• Description: Description of your choice
• Property type: Input
• Data type: Boolean
9) Add one more custom property with the following values and click Create. Make sure this
property is an output property type.
• Display name: Selected
• Name: Selected
• Description: Description of your choice
• Property type: Output
• Data type: Record
11) Select the Data property of the Carousel and replace the value with formula below. This sets a
default value for the Data property and will allow you to build the component using these
column names.
Table({ Record: "Contact", Values:Table({Col:1,Value:"Full Name"}, {Col:2,
Value:"Address 1: Street 1"}) })
11
Reusable Components- Lab Overview
2) Select the Insert tab, click Text and select HTML text.
8) Select the Insert tab, click Icons and select Left. We are adding left and right icons to allow
the user of the app to manually change the item displayed.
13
Reusable Components- Lab Overview
14) Select the OnSelect property and set it to the value shown below. This will cause the prior
record to be shown if there is one.
If(CurrentIndex > 1,Set(CurrentIndex,CurrentIndex-1))
15) Select the Insert tab, click Icons and select Right.
htmlText.Y
21) Select the OnSelect property and set it to the formula below. This will advance to the next
record as long as we don’t exceed the number of records in the Data property.
If(CurrentIndex < CountRows(Parent.Data),Set(CurrentIndex,CurrentIndex+1))
22) Your component should now look like the image below.
23) Select the Insert tab, click Inputs and select Toggle.
You are adding this control to simulate behavior properties, we need a way to run a formula
when the data changes. The data property doesn’t have an OnChange property so we are
asking the hosting app to change the RefreshData property when there is new data. We will
then hookup the RefreshData to the toggle. And finally, our formula will run OnChange of the
toggle. You will hide it later because the user never needs to see it, we are just using it to allow
running our formula on-demand.
15
Reusable Components- Lab Overview
25) Set the OnChange property of the toggle button to the formula below. When the hosting app
sets the RefeshData property to indicate new data has been set this sets the index variable
back to 1
Set(CurrentIndex,1)
26) Set the Default property of the toggle button to the formula below. This hooks up the
RefreshData property to the hidden toggle control. When the hosting app changes this
property value it will cause the OnChange formula above to execute. We are using the toggle
control simply to allow the hosting app to run a formula on demand because components don’t
have a way to directly have a hosting app run a component formula
Parent.RefreshData
27) Click Inputs again and select Slider. You are adding this control to simulate behavior
properties. You will hide it later.
32) Set the slider OnChange property to the formula below. This runs when the CurrentIndex
variable is changed.
If(CountRows(Parent.Data) > 0,ClearCollect(
TemplateData,
{
Name: "HtmlTemplate",
Template: Parent.Template
}
);Set(CurrentRecord,Last(FirstN(Parent.Data, CurrentIndex)));
ForAll(
CurrentRecord.Values,
Patch(
TemplateData,
LookUp(
TemplateData,
Name = "HtmlTemplate"
),
{Template: Substitute(LookUp(TemplateData,Name =
"HtmlTemplate").Template,"{"& Col &"}",Value)}
)
);Set(OutputHtml,LookUp(TemplateData,Name = "HtmlTemplate").Template))
The above formula does the following (Each bullet below represents one of the
forumlas above):
• Creates a copy of the HTML template so we can customize it for this record.
• Sets CurrentRecord varaiable based on the CurrentIndex. CurrentIndex represents the
record number in the set we want to display. You can’t explictly get a record in a Table, so
we are using Last and FirstN to help get the specific record. For example if CurrentIndex is
7 and we had 10 records, FirstN of 7 would return us the first 7 records. Last on that
would give us just the 7th record. Using this technique you can access a specific record in
a set by an index value.
• Loops through all the values that we want to replace in the template, and for each calls
Subsitute to replace the value in the template.
• Sets the OutputHtml property with the results and later we will use this to populate the
Html Text control.
17
Reusable Components- Lab Overview
35) Your component will now look like the image below.
2) Select the MainScreen, go to the Insert tab, click Custom, and select the Carousel component
you created.
3) Set the Template property to the formula below. This is the HTML that will be used to format
each record. {1} and {2} are replaceable with data from the record being visualized. This is a
very simple template, it could include more styling and layout as needed.
"<H1>{1}</H1><H2>{2}</H2>"
5) Select accountsList and set the OnSelect property to the formula below.
Set(RefreshContacts, false );Clear(SelectedContacts);
ForAll(accountsList.Selected.Contacts,Collect(SelectedContacts,
{Record:contactid,Values:Table({Col:1,Value:fullname},{Col:2,Value:jobtitle})})
);Set(RefreshContacts, true);
This is preparing the data for the component formatting the contact data into the format
expected.
19
Reusable Components- Lab Overview
6) Place the carousel component on the screen like the image below.
7) Click Play.
8) Select an account with more than one contact. Alpine Ski House has two related contacts. The
Carousel should load the first contact.
21
Reusable Components- Lab Overview
3) Change the timer Duration to 5 seconds. The unit of measure is milliseconds, so to set to 5
seconds, the value you set is 5000.
4) Set the timer OnTimerEnd property to the formula below. We are using the timer control to
automate rotating the current record being shown by the carousel. Every 5 seconds (based on
how you configure the timer) the formula you configure for OnTimerEnd will run. We are
using that to increment the index if it is less than the # of records in the data collection and if it
reaches the end we are setting it back to the beginning.
If (CurrentIndex <
CountRows(Parent.Data),Set(CurrentIndex,CurrentIndex+1),Set(CurrentIndex,1))
7) Set the X property of the timer to the formula below. Next, we are going to adjust the timer
play button so if the hosting app changes the size of our component the size of the button will
adjust.
(Parent.Width / 2) - (Timer1.Width/2)
9) Set the Visible property of the toggle and slider controls to false. Since we are using both
these controls simply to call our formulas, we can hide them from the user.
10) Your component should now look like the image below.
23
Reusable Components- Lab Overview
18) The contacts should rotate based on the 5 seconds duration you selected.
19) Close the preview.
25
Reusable Components- Lab Overview