Module 6: Data Binding Fundamentals
Data binding is the core strength of SAPUI5.
It automatically synchronizes UI controls and application data, eliminating manual DOM manipulation.
If you master data binding, UI5 becomes easy and powerful.
1. What is Data Binding in SAPUI5?
Data binding connects:
- Model data
- UI control properties / aggregations
Any change in the model is automatically reflected in the UI.
No manual UI refresh is required.
2. Property Binding
What is Property Binding?
Property binding binds a single model value to a control property.
Example
<Input value="{/username}" />
If /username changes in the model → input updates automatically.
With Type & Validation
<Input
value="{path: '/amount', type: 'sap.ui.model.type.Float'}" />
Use data types for formatting and validation.
3. Aggregation Binding
What is Aggregation Binding?
Aggregation binding binds a collection (array) to a control aggregation like:
- List items
- Table rows
Example: List Binding
<List items="{/employees}">
<StandardListItem title="{name}" description="{role}" />
</List>
How It Works
Model Array → UI5 creates items dynamically
UI5 handles creation and destruction of child controls.
4. Element Binding
What is Element Binding?
Element binding binds a single object as a context for multiple controls.
Example
<VBox binding="{/employee}">
<Text text="{name}" />
<Text text="{department}" />
</VBox>
Controller-Based Element Binding
this.getView().bindElement("/employee");
Detail views in Master–Detail applications.
5. One-Way vs Two-Way Binding
Binding Modes
| Mode | Description |
|---|---|
| OneWay | Model → UI |
| TwoWay | Model ↔ UI |
| OneTime | Initial value only |
Default Behavior
| Model | Default Mode |
|---|---|
| JSONModel | TwoWay |
| ODataModel | TwoWay |
Explicit Binding Mode
oModel.setDefaultBindingMode(sap.ui.model.BindingMode.OneWay);
Two-way binding can unintentionally modify model data.
6. Formatter Functions
What is a Formatter?
Formatter functions:
- Transform data before display
- Keep logic out of views
XML Usage
<Text
text="{path: '/price', formatter: '.formatPrice'}" />
Formatter Implementation
formatPrice: function (fPrice) {
return fPrice + " USD";
}
Use formatters only for presentation logic, not business logic.
7. Expression Binding (Modern & Powerful)
What is Expression Binding?
Expression binding allows inline logic in XML using {= }.
Examples
<Text text="{= ${/status} === 'A' ? 'Active' : 'Inactive' }" />
<Button enabled="{= ${/amount} > 0 }" />
Reduces controller code for simple conditions.
When to Use Expression Binding
- Simple conditions
- Visibility toggles
- Enable/disable logic
Complex logic in expression bindings.
8. Binding Context & Paths
Absolute vs Relative Paths
| Path Type | Example |
|---|---|
| Absolute | /employees/0/name |
| Relative | name (with context) |
Relative paths depend on binding context.
9. Multiple Models & Named Models
<Text text="{userModel>/name}" />
Using multiple models is very common.
10. Best Practices for Data Binding
- Prefer declarative bindings
- Use element binding for detail views
- Use formatters for display logic
- Use expression binding sparingly
- Keep business logic out of views
- Manual DOM updates
- Deep logic in XML
- Unnecessary two-way binding
- Excessive formatter usage
11. UI5 Data Binding vs Classical JS
| Aspect | Classical JS | SAPUI5 |
|---|---|---|
| UI updates | Manual | Automatic |
| Binding | Custom | Built-in |
| State mgmt | Manual | Model-based |
Data binding reduces bugs and improves maintainability.
12. Interview-Grade Explanation
Q: Explain different types of data binding in SAPUI5.
Answer:
SAPUI5 supports property, aggregation, and element binding. Property binding connects single values, aggregation binding handles collections like lists, and element binding sets a context for multiple controls. Binding modes define data flow, and formatters and expression bindings handle presentation logic.
13. Summary
- Data binding is core to UI5
- Property binding handles single values
- Aggregation binding handles collections
- Element binding simplifies detail views
- Binding modes control data flow
- Formatters & expression bindings format data
14. What's Next?
➡️ Module 7: Models in SAPUI5
Once data binding clicks, UI5 feels magical.