Lambda Expressions
Remove boilerplate from your applications with template lambda expressions.
Lambda expressions in Aurelia templates offer a concise and powerful way to include JavaScript-like functionality directly within your HTML using a subset of Arrow Function syntax. This feature enhances the expressiveness of Aurelia's templating engine, allowing for more inline, readable, and maintainable code.
Array methods with repeat.for
One of the most common scenarios will likely be calling Array methods on an array you're looping over using repeat.for
.
Previously, to filter some array items in a repeater, you might have written something like this using a value converter:
While there is nothing wrong with value converters, and in some situations, they might be preferable (especially for testing), you can achieve the same thing without writing any additional code like this:
We are calling a callback function called isGood
defined inside our template to determine if the item is filtered.
Filter and Sort
Observation-wise, Aurelia knows only to observe selected
property of every item in items
, as well as pos
property of every selected item. This means changing the value of selected
property of any item will result in the re-evaluation of the above expression. Changing the value of pos
property of any selected item will also trigger the re-evaluation. Aurelia will also subscribe to the mutation of the array items
to refresh this binding.
Methods on array that will create an array subscription
map
filter
includes
indexOf
lastIndexOf
findIndex
find
flat
flatMap
join
reduce
reduceRight
slice
some
Methods that trigger self-mutation like sort
/splice
/push
/pop
/shift
/unshift
/reverse
will not result in a subscription. It's unclear when and how to refresh the binding.
For sorting, it is recommended that we create a new array with slice
before sorting: items.slice(0).sort(...)
since sort()
mutates the existing array and could sometimes make the outcome confusing to follow.
As we might have inside of a value converter, we use two Javascript functions, filter
and sort
— Aurelia's lambda expression support means we can chain these functions without needing to write any code in a view-model or value converter.
Event Callbacks
With arrow functions, we can express the following:
As the following:
As a result, .call
is being deprecated in Aurelia as the lambda expression syntax allows us to handle this in a more JavaScript way.
Interpolation Expressions
Not only are lambda functions supported in a repeat.for
, but we can also use them in interpolation expressions.
Map
Say you have an array of keywords for an item, and you want to display those as a comma-separated list. Previously, you would have used a value converter or function in your view model to achieve this task. Now, you can do it from within your templates.
Reduce
Another task might be to take an array of items (say, products in a cart) and then calculate the total. Once again, we might have previously used a value converter or computed getter for this task, but now we can use reduce
in our template.
Valid Uses
While a broad syntax for lambda expressions is supported, here is a list of valid uses.
Invalid Uses
The following uses of lambda expressions are not supported in Aurelia templates.
Examples
Now we understand what lambda expressions are and how they can be used, here are some examples of how you might leverage them in your Aurelia applications.
Inline Array Transformation and Display
Transform and display an array of objects inline, such as a list of names.
Complex Object Filtering
Use lambda expressions for complex filtering, such as filtering based on multiple object properties.
Event Handling with Additional Context
Handle events with additional context passed to the event handler.
Nested Array Operations
Perform operations on nested arrays, such as displaying a flattened list of sub-items.
Combining Multiple Array Methods
Chain multiple array methods for data processing.
Using Reduce for Total Calculation
Calculate the total price of items in a shopping cart.
Creating a Comma-Separated List
Transform an array of objects into a comma-separated list.
Calculating an Aggregate Property
Use reduce
to calculate an aggregate property, such as the average age.
Using includes
for Conditional Rendering
includes
for Conditional RenderingCheck if an array includes a certain value and conditionally render content.
Dynamic Class Assignment with includes
includes
Assign a class to an element if an array includes a specific item.
Finding an Item with find
find
Display details of the first item that matches a condition.
Using find
in Event Handling
find
in Event HandlingUse find
in an event handling expression to work with a specific item.
Nested Arrays with flat
flat
Flatten a nested array and display its contents.
Displaying a Flattened List of Attributes
Use flat
to create a flattened list of attributes from an array of objects.
Last updated