Synthetic view
Learn how you can dynamically synthesize views from templates generated on runtime.
Synthetic views in Aurelia 2 are a powerful feature that allows developers to create and manage views programmatically at runtime. Unlike traditional views in HTML templates, synthetic views offer complete control over view creation, binding, and lifecycle management.
When to Use Synthetic Views
Synthetic views are particularly useful when:
Rendering server-generated HTML with Aurelia bindings
Dynamically creating templates based on runtime data
Integrating with CMS or third-party content that needs Aurelia binding capabilities
Creating complex, programmatically-generated UIs
Basic Concepts
A synthetic view in Aurelia 2 consists of several key components:
Template Creation: A programmatically created template element containing your dynamic HTML
Render Location: A marker in the DOM where Aurelia will render your view
View Factory: Creates view instances from your template
Scope: Provides the binding context for your view
View Instance: The actual view that gets rendered and managed
Prerequisites
Before working with synthetic views, you should be familiar with:
Creating a new Aurelia app. This tutorial won't cover this; you can look at the other tutorials.
You have familiarized yourself with the Aurelia template syntax.
You have familiarized yourself with components in Aurelia.
You are familiar with Dependency Injection. You don't need to master it; you need to be familiar with its existence and why it matters in Aurelia.
Native Web APIs, such as
createElement.
Getting Started
Here's a basic example of creating a synthetic view:
This code demonstrates the basic pattern for creating and managing synthetic views in Aurelia 2. Each step is essential:
Template Creation: Create a template element with your dynamic content
DOM Integration: Add the template to the DOM and create a render location
Factory Creation: Create a view factory from your template
View Creation: Create a view instance and set its location
Activation: Activate the view with a scope containing your view model
Cleanup: Properly deactivate the view when done
Working with Scopes and Bindings
Understanding how to handle scopes and bindings properly is crucial when creating synthetic views. The scope provides the context for all bindings within your dynamic template.
Basic Scope Handling
Here's how to create and use scopes effectively:
Working with Parent Scopes
Sometimes, you need to access the parent component's scope in your synthetic view. Here's how to do that:
This pattern keeps the parent scope intact by calling Scope.fromParent(this.$controller.scope, childBindingContext) and then referencing the parent via $parent inside the synthetic template. The child binding context stays isolated (localMessage), while $parent.message always reflects the latest value on the host element.
Handling Dynamic Updates
When your view model data changes, the bindings will automatically update. However, if you need to rebuild the view completely, you'll need to handle that manually:
Best Practices for Scope Management
Cleanup: Always deactivate views when they're no longer needed
Scope Isolation: Create isolated scopes when you don't need parent scope access
Parent Scope Access: Use parent scopes judiciously to avoid tight coupling
Memory Management: Clear references to views and scopes when disposing
Error Handling: Wrap view creation and activation in try-catch blocks
Real-World Examples
1. Server-Rendered Content with Aurelia Bindings
This example shows how to take HTML content from a server (like a CMS) and make it interactive with Aurelia bindings:
2. Dynamic Form Builder with Validation
This example creates forms dynamically based on a schema, with validation:
Usage:
Advanced Topics and Patterns
1. Dynamic Component Loading with Lazy Loading
This example shows how to load and render components based on server configuration dynamically:
2. Interactive Dashboard Builder
This example demonstrates building a dashboard with draggable widgets:
Last updated
Was this helpful?