Module 7: Models in SAPUI5
Models in SAPUI5 are responsible for holding application data and state.
They act as the single source of truth and drive UI updates via data binding.
A well-designed UI5 application typically uses multiple models, each with a clear purpose.
1. What is a Model in SAPUI5?
A model:
- Holds data (JSON, XML, OData, texts)
- Exposes data to views
- Works with data binding
- Decouples UI from data source
Views never "own" data — models do.
2. JSON Model
What is JSONModel?
JSONModel is the most commonly used client-side model in UI5.
Use cases:
- UI state
- Temporary data
- Calculated values
- Mock data
Creating a JSONModel
var oModel = new sap.ui.model.json.JSONModel({
name: "Yogesh",
age: 30
});
Setting JSONModel on View
this.getView().setModel(oModel);
Binding JSONModel Data
<Text text="{/name}" />
<Input value="{/age}" />
Use JSONModel for UI state, not persistent backend data.
3. Resource Model (i18n)
What is ResourceModel?
ResourceModel is used for internationalization (i18n).
Purpose:
- Language-dependent texts
- Avoid hardcoded strings
- Support multi-language apps
i18n File Example (i18n.properties)
appTitle=User Management
saveButton=Save
errorMessage=An error occurred
ResourceModel Configuration (manifest.json)
"sap.ui5": {
"models": {
"i18n": {
"type": "sap.ui.model.resource.ResourceModel",
"settings": {
"bundleName": "com.app.demo.i18n.i18n"
}
}
}
}
Binding i18n Texts
<Button text="{i18n>saveButton}" />
Never hardcode texts in UI5 applications.
4. XML Model (Intro)
What is XMLModel?
XMLModel holds data in XML format.
Use cases:
- Legacy systems
- Static configuration data
Example (Rare Usage)
var oXmlModel = new sap.ui.model.xml.XMLModel("data.xml");
this.getView().setModel(oXmlModel);
XMLModel is rarely used in modern applications.
5. Model Scope: View vs Component
Model Scope Explained
Models can be set at different levels:
| Scope | Visibility |
|---|---|
| View | Only that view |
| Component | Entire app |
| Control | Single control |
View-Level Model
this.getView().setModel(oModel);
Use when:
- Data is local to the view
- Temporary UI state
Component-Level Model (Recommended)
this.getOwnerComponent().setModel(oModel);
Use when:
- Data is shared across views
- Global application state
Prefer Component-level models for shared data.
6. Named Models
Why Named Models?
Named models allow:
- Multiple models in one app
- Clear separation of concerns
Setting Named Model
this.getView().setModel(oModel, "userModel");
Binding Named Model Data
<Text text="{userModel>/name}" />
Typical Named Models
| Model Name | Purpose |
|---|---|
| (default) | Main data |
| i18n | Texts |
| viewModel | UI state |
| device | Device info |
Using a dedicated viewModel is a common best practice.
7. Device Model (Common Pattern)
Device Model Usage
var oDeviceModel = new sap.ui.model.json.JSONModel(sap.ui.Device);
oDeviceModel.setDefaultBindingMode("OneWay");
this.getView().setModel(oDeviceModel, "device");
Binding Device Properties
<Button visible="{= !${device>/system/phone} }" />
Device model helps adapt UI without hardcoding logic.
8. Model Lifecycle & Updates
Updating Model Data
oModel.setProperty("/name", "New Name");
UI updates automatically.
Refreshing Model
oModel.refresh(true);
Avoid frequent full refreshes.
9. Best Practices for Using Models
- Separate UI state and backend data
- Use i18n for all texts
- Prefer component-level models
- Use named models for clarity
- Keep models simple and focused
- Hardcoding texts
- Using one model for everything
- Storing business logic in models
- Excessive model refreshes
10. Interview-Grade Explanation
Q: What models do you typically use in a SAPUI5 application?
Answer:
I typically use ODataModel for backend data, JSONModel for UI state and temporary data, ResourceModel for i18n texts, and named models to clearly separate concerns and improve maintainability.
11. Summary
- Models store application data
- JSONModel handles client-side data
- ResourceModel enables i18n
- XMLModel is legacy/rare
- Model scope defines visibility
- Named models improve structure
12. What's Next?
➡️ Module 8: Routing & Navigation
Well-structured models make complex UI5 apps easy to manage.