Spread Binding (.spread)
The .spread binding command allows you to bind multiple properties from an object to a custom element's bindable properties or to an HTML element's attributes in a single, concise expression. This is particularly useful when you have an object with multiple properties that match the bindable properties of a custom element or attributes of an HTML element.
Overview
Instead of binding each property individually:
<user-card
name.bind="user.name"
email.bind="user.email"
avatar.bind="user.avatarUrl"
role.bind="user.role">
</user-card>You can use .spread to bind all matching properties at once:
<user-card user.spread="user"></user-card>Basic Usage with Custom Elements
The .spread binding is most powerful when used with custom elements that have multiple bindable properties:
// user-card.ts
import { bindable } from 'aurelia';
export class UserCard {
@bindable name: string;
@bindable email: string;
@bindable avatarUrl: string;
@bindable role: string;
}Result: The user-card component receives all matching properties from currentUser. Properties like name, email, avatarUrl, and role are automatically bound to their respective bindable properties.
How It Works
The .spread binding:
Evaluates the expression to get an object
Identifies matching properties between the object and the target's bindable properties (for custom elements) or valid attributes (for HTML elements)
Creates individual bindings for each matching property
Updates dynamically when the source object changes
Property Matching
For custom elements, only properties that are declared as @bindable are bound:
For HTML elements, all standard HTML attributes can be bound:
Dynamic Object Updates
When the source object changes, the bindings update automatically:
Combining Spread with Individual Bindings
You can mix .spread bindings with individual property bindings. Individual bindings take precedence:
Result: The user-card receives name and role from userDefaults, but email is bound to adminEmail specifically.
Spreading HTML Element Attributes
The .spread binding works with regular HTML elements too:
Spreading to Multiple Custom Elements
You can use the same object with different custom elements:
Each component receives only the properties it defines as @bindable.
Strict Mode
By default, .spread operates in non-strict mode, which means it silently ignores properties that don't match bindable properties. In development builds, warnings may be logged when spreading non-object values.
Performance Considerations
Efficient Updates
The .spread binding uses caching to minimize unnecessary work:
Binding cache: Bindings for each property are created once and reused
Scope cache: Binding scopes are cached per object instance
Smart updates: Only properties that exist in both the source object and target are bound
When to Use Spread
Use .spread when:
You have an object with multiple properties that map to bindable properties
You're working with data from APIs or state management
You want to reduce template verbosity
The source object structure matches the target's bindable properties
Avoid .spread when:
You only need to bind one or two properties (use individual bindings)
You need fine-grained control over each binding's mode (
.two-way,.one-time, etc.)The source object has many properties that don't match the target (creates unnecessary overhead)
Complete Example: Dynamic Form
Here's a comprehensive example showing how to build dynamic forms with .spread:
Spreading with Repeaters
Combine .spread with repeat.for to dynamically generate components from data:
Nested Spread Bindings
The .spread binding can be nested to handle captured attributes in parent-child custom element scenarios:
The spread binding will automatically handle captured attributes and pass them through the component hierarchy. This is particularly useful when building wrapper components.
Integration with State Management
Spread bindings work naturally with state management solutions:
Debugging Spread Bindings
In development mode, you can inspect which properties were bound:
If properties aren't binding as expected:
Verify @bindable decorators: Ensure target properties are decorated with
@bindableCheck property names: Property names must match exactly (case-sensitive)
Inspect the source object: Log the object being spread to verify it contains expected properties
Watch for warnings: Development mode logs warnings for non-object values
Comparison with Other Approaches
Traditional Individual Bindings
Pros: Clear, explicit, supports different binding modes per property Cons: Verbose, repetitive, harder to maintain
Spread Binding
Pros: Concise, reduces repetition, easy to maintain Cons: Less explicit, all properties use .to-view binding mode
Passing Whole Object as Bindable
Pros: Very concise, passes all data Cons: Requires component to accept object, tighter coupling
Best Practices
Use descriptive names: Name the spread binding to indicate what it spreads (e.g.,
user.spread,config.spread,props.spread)Document expected properties: In your custom element, document which properties are expected from a spread binding
Provide defaults: Use default values for optional properties
Validate in lifecycle hooks: Validate required properties in
binding()orbound()hooks
See Also
Bindable Properties - Defining bindable properties on custom elements
Custom Elements - Building custom elements
Attribute Binding - Standard attribute binding syntax
Property Binding - Binding to component properties
Last updated
Was this helpful?