Globals
Learn how Aurelia 2 handles global variables in templates, the built-in list of accessible globals, and when to use them effectively.
By design, Aurelia templates limit direct access to global variables like window
or document
for security and maintainability reasons. However, Aurelia recognizes that some JavaScript globals are frequently needed—like Math
, JSON
, or Array
—and therefore provides a predefined list of global objects that can be safely accessed in template expressions.
Why Limit Global Access?
Security: Restricting direct access to browser globals reduces the risk of accidental or malicious operations on sensitive objects.
Maintainability: Encourages developers to keep logic in their view models, improving code clarity.
Performance: Minimizes the amount of unnecessary logic in templates, preventing overuse of global operations in tight rendering loops.
Despite these constraints, Aurelia acknowledges the utility of common global constructors and functions. Below is the canonical list accessible within Aurelia 2 templates without additional configuration:
Infinity
NaN
isFinite
isNaN
parseFloat
parseInt
decodeURI
decodeURIComponent
encodeURI
encodeURIComponent
Array
BigInt
Boolean
Date
Map
Number
Object
RegExp
Set
String
JSON
Math
Intl
Example Usages of Built-In Globals
Below are illustrative examples showing how to use these built-in globals in Aurelia templates. The syntax is identical to standard JavaScript, but you simply call them within Aurelia’s binding expressions.
1. Working with JSON
JSON
Serialize an object for debugging or quick display:
2. Mathematical Operations with Math
Perform simple or complex calculations:
3. Conditional Rendering with isNaN
Use global numeric checks to conditionally display elements:
4. Regular Expressions with RegExp
Construct inline regular expressions for quick validation:
5. Dynamic Property Access with Object
Use Object methods for reflection or retrieval:
6. Set Operations with Set
De-duplicate arrays or combine sets inline:
7. Encoding & Decoding URLs
Leverage encodeURI / decodeURI for safe link construction:
8. Number Formatting with Intl.NumberFormat
Localize numbers, currency, or dates easily:
9. Complex Array Manipulations
Filter, map, and transform arrays:
Best Practices and Considerations
Use Sparingly
Keep business logic in your view models, not in templates. Inline calls to complex global functions (e.g., JSON.stringify on large data) can degrade performance and reduce readability.
Security
Even though Aurelia limits global access, treat any data you process via global functions (e.g., decodeURI) with caution to prevent potential XSS attacks or other vulnerabilities.
Performance
Template expressions run on each re-render. If you repeatedly perform expensive operations (like JSON.stringify on large objects), consider handling them in the view model and binding to a computed property instead.
Reactivity
Accessing global objects doesn’t magically become reactive. If you want to update the UI when data changes, store and manipulate it in the view model, ensuring Aurelia’s change detection can pick it up.
Clarity and Testing
Test heavy logic in a view model or service, not in templates. This approach keeps your code testable with unit tests and fosters a separation of concerns.
By sticking to these guidelines, you can leverage Aurelia’s built-in global access without sacrificing maintainability or performance.
Last updated
Was this helpful?