Globals
In Aurelia 2, templates are designed to limit direct access to global variables like window
or document
for security reasons. However, there are scenarios where access to certain global variables is necessary. Aurelia allows access to a predefined list of global variables commonly used in development.
To reduce boilerplate, Aurelia allows template expression to access a fixed list of global variables that are usually needed. Those globals are as follows:
Global Examples
Here are some examples of using globals inside of Aurelia templates. The usage is the same as it is inside of Javascript, except in your Aurelia templates.
Working with JSON
Manipulate JSON data directly in your templates.
Mathematical Operations
Perform calculations using the Math object.
Conditional Rendering with isNaN
isNaN
Display content conditionally based on numeric checks.
Regular Expressions with RegExp
RegExp
Use the RegExp
constructor to create dynamic regular expressions for data validation or manipulation.
Dynamic Object Property Access with Object
Object
Access properties dynamically on an object using the Object
constructor.
Set Operations with Set
Set
Demonstrate set operations like union, intersection, or difference.
Encoding and Decoding URLs with encodeURI
and decodeURI
encodeURI
and decodeURI
Manipulate URL strings by encoding or decoding them.
Number Formatting with Intl.NumberFormat
Intl.NumberFormat
Format numbers using Intl.NumberFormat for localization.
Complex Array Manipulations with Array
Array
Demonstrate complex array manipulations, such as chaining methods.
Best Practices and Considerations
Use Sparingly: Rely on global variables only when necessary. Prefer component properties and methods for data and logic.
Security: Be cautious of the data processed using global functions to prevent XSS attacks and other vulnerabilities.
Performance: Frequent use of complex operations like JSON.stringify in templates can impact performance. Consider handling such operations in the component class.
Reactivity: Remember that changes to global variables are not reactive. If you need reactivity, use component properties or state management solutions.
Last updated