For a Swing or JavaFX developer switching between views in Single Page Applications (SPAs) might be a little strange at first. “Single Page” seems to imply, that everything needs to be in one large HTML-File. But that would be ugly, especially as applications tend to grow over time. Let’s have a look how we can structure an application with separate views. Let’s start with how to switch between different views.
Switching Views with in-place templates
The easiest way to switch between two views is to hide or show either view only when a condition is met. As an example let’s take the viewmodel from this demo application:
With this model we can switch the view when someone is editing a Task. Here’s the code that switches the view for the editing row in a list:
If the current item ($data) in the foreach loop is the editing Task of the viewmodel, a textfield is displayed. We have defined two different templates for displaying a task and depending on the condition “$root.editing()===$data” we display one of them. This is called in-place templates, as the templates are defined where they are used. This approach is good for small templates, and it’s very easy to understand what is happening. But this approach has limitations:
If the templates are getting bigger, readability suffers. Also there’s no way to reuse an in-place template. That’s when we start to use the template binding.
Switching Views with Template Binding
First we simply move the in-place templates to a script tag and give them an id. At the original location we place a tag with a template binding. The name parameter references the template id:
This increases readability for large templates and allows us to reuse the same template at different locations. As an example we might use the same template for editing an existing item and for adding a new one.
If the application grows larger, you might want to put the templates into an external file. This keeps the html files small and allows you to further structure the application. There are several ways of doing this. We suggest you try our dynamic templates.
A while ago we’ve announced dynamic templates, a small helper project that allows you to register templates at runtime. The immediate benefit of this project is, that you can load templates lazily.
The github repository contains an example app using the API. To register a template you will make a call to a Java API:
The template will only be loaded from an external file, when you first use it in a template binding.
Switching templates is only one part of the story. If you have a DukeScript based webapp, you also want to allow users to use bookmarks and history to navigate your app. In this blog entry we’ve described how you can do some simple routing with a DataModel that holds the current page, and a special “route” binding.
The DataModel looks like this:
If you combine that approach with dynamic templates you can store your templates in separate html files and control which page is active through the page property of your datamodel:
Please note how the template binding’s name attribute is bound to the page property of the DataModel.
I hope this gives you some ideas how you can use templates and switch between views.