Show TOC

Model InstantiationLocate this document in the navigation structure

The app configures several data models that are used throughout the app to update the views or to store additional configuration options.

The service model and the resource bundle are instantiated automatically by the app’s component during startup and described in the first section. The local view models and helper models such as the device model are set up as JSON models and described in the second section.

Automatic Model Instantiation

The templates instantiate the service and resource model automatically using the following configuration entries in the descriptor. When the component of the app is initialized, these models will be made available under the configured name throughout the app.

An external service is defined in the dataSources section of the sap.app namespace. In the example shown below, we configure an OData with protocol version 2.0 and the alias "mainService" in the manifest.json file:
{
	…
	"sap.app": {
		…
		"i18n": "i18n/i18n.properties",
		…
		"dataSources": {
			"mainService": {
				"uri": "/here/goes/your/serviceUrl/"
				"type": "OData",
				"settings": {
					"odataVersion": "2.0",
					"localUri": "localService/metadata.xml"
				}
			}
		},
		…
	},
	…
}
In the models section of the sap.ui5 namespace we define two models that will be instantiated automatically. The resource model is a named model ( i18n ) and the OData model is the default model so it has no name. The OData model also receives additional URL parameters via the metadataUrlParams. The parameters sap-server, sap-client and sap-language are passed to the service automatically by SAPUI5, as shown in the following manifest.json code snippet:
{
	…
	"sap.ui5": {
		…
		"models": {
			"i18n": {
				"type": "sap.ui.model.resource.ResourceModel",
				"settings": {
					"bundleName": "sap.ui.demo.i18n.i18n"
				}
			},
			"": {
				"dataSource": "mainService",
				"settings": {
					"metadataUrlParams": {
						"sap-documentation": "heading"
					}
				}
			}
		},
		…
	}
}
Note

Before SAPUI5 release 1.30, all models were defined and instantiated in the component's init method. We recommend removing all manual model creation code and switching to the automatic model instantiation instead. The "device model" however is still a local model that has to be instantiated manually.

Additional Models for the App

The following models are created as local JSON models in the app and can be referenced by its model name where needed:

  • device

    The device model provides an easy access to the SAPUI5/OpenUI5 device API and is used to configure certain view settings according to the user’s device.

  • FLP

    The FLP model is a helper module to configure SAP Fiori launchpad (FLP) integration and is used to control the sharing options of the app.

  • worklistView

    A local view model for the worklist view that stored configuration options that are bound to controls in the view.

  • objectView

    A local view model for the object view that stored configuration options that are bound to controls in the view.