Contents
What are we going to build?
In this tutorial we are going to build an app where input entered on the first is saved into browser’s local storage and then displayed on the second page.
Before you start
Tutorial level: Beginner
Prerequisites: Tiggzi account
Creating new app
Create a new app, call it: HelloWorld. When the builder loads, open startScreen page:
Now that we have the app, let’s design the first page.
Building the input page
On the first page we are going to have an input and a button to save the value into local storage:
- Change the caption in the header to: App (or anything else you want)
- Drag and drop an Input component.
- In Properties, check Placeholder
- In Properties, change the component Name to: Input
- Drag and drop a Button component, change its label to Save
- In Properties, change the button name to: saveButton
You can click Test, to open the app in web browser.
Building the result page
- From Project view, Create New > Page
- For page name enter: Result
- Click Create Page
- Build the following UI:
- To add a back button to the header, select the header and check Back Button option
- For the Label component, change its name to: Output
We are done with the UI. Next we are going to work on saving data into local storage and displaying it on the Result page.
Saving data into local storage
- Open startScreen
- Select the button and open Events tab. We are going to save user entered input into local storage when the button is clicked.
- The Component and Event should be set (button defaults to click event, but you can always change it):
If not set, select saveButton from Component list and Click from Event. - From Action list, select Set local storage variable action
- For Variable name enter: text. That’s the name of the local storage variable
- Check Bind to Component. This will allow us to get the value from the input element
- Target component select: Input
- Property name select: Text
We are taking the value from the input element and saving it into local storage named: text
- Click the green check icon to save.
After we save the data into local storage, we need to navigate to the second page.
- Select the button
- Open Events tab
- With Compoent set to saveButton and event to Click, select Navigate to Page action
- For Page, select Results
- Click the green plus icon to save.
The button now has two actions:
Reading data from local storage
- Open Result page. When the page load, we would like to read the value from local storage and display it using the Label component.
- Open Events tab
- From Component list select: Result. That’s the page
- From Event, select: Load
- From Action, select: Set property
- For Component name, select: Output
- For Property name, select: Text
- Check Read from local storage variable
- For Value, select: text
- To save this event, click the green plus icon.
We got the back button in the header. As this is a default back button from jQuery Mobile, it will automatically navigate to the previous page so we don’t need to define any events for it.
Testing the app
Now that we are done with the app, go ahead and test in the browser. The app should work like this:
Result page:
Using local storage API directly (optional)
Tiggzi provides action such as Create local storage variable, Navigate to page - in order to make it simpler and faster to build apps. Behind these actions are standard local storage API calls. This part is optional but you will get a good idea how the app works.
Modifying startScreen page
- Open startScreen
- Select the button and open Events. You will see the two actions we defined.
- Delete both actions
- Still using the Click event, add Run JavaScript action with the following code:
var input = Tiggzi('Input').val(); localStorage.setItem('text', input); Tiggzi.navigateTo('Result', {});The first line reads the value from input field. Learn more about Tiggzi JavaScript API. It’s also possible to use plain jQuery API. The second line uses the browser local storage API to save user input. The third line navigates to the second page.
- Save.
- Open Result page
- Open Events
- Delete the Load event (if you don’t see the event, click Show All)
- Add a new Load event to the page but the action this time is Run JavaScript.
- Enter the following code:
var text = localStorage.getItem('text'); Tiggzi('Output').text(text);The first line reads the value from local storage. This is just using the browser API.
The second line sets the value to the label. - Save.
- Go ahead and test the app, the result will be identical.