Skip to Content
1
Aug 28, 2017 at 01:38 PM

Dynamically replace default OData Model

2605 Views Last edit Aug 28, 2017 at 06:07 PM 2 rev

Hello SAP UI5 Experts,

I have a SAPUI5 Applicaiton that has a default OData Model defined in the manifest.json

"": {
	"dataSource": "mainService",
	"settings": {
		"metadataUrlParams": {
			"sap-documentation": "heading"
		},
		"defaultBindingMode": "TwoWay"
	}
}

now we want to use the same application against different OData Services that implement different authorizations. The interface of the service is always the same. For that I've implemented the following function in the Component.js of my App:

_setDefaultModelForUser: function () {
	var that = this;
	var _odata1 = "/group1/service.xsodata";
	var _odata2 = "/group2/service.xsodata";
	var serviceUrl = "";
	var oManifest = this.getManifestObject();
	var oCurrentUser = this.getModel("currentUser");
	oCurrentUser.attachRequestCompleted(function() {
		var username = oCurrentUser.getProperty("/name");
		
		switch(username) {
			case "P598692":
				serviceUrl = _odata1;
				break;
			case "S0001142741":
				serviceUrl = _odata2;
				break;
			default:
		}
		
		if(serviceUrl !== "") {
			oManifest._oManifest["sap.app"].dataSources.mainService.uri = serviceUrl;
			var oDefaultModel = that.getModel("");
			var defaultModelSettings = oManifest._oManifest["sap.ui5"].models[""].settings;
			oDefaultModel = new sap.ui.model.odata.v2.ODataModel(serviceUrl, defaultModelSettings);
			that.setModel(oDefaultModel, "");
		}
	});
}


I call this method at the beginning of the init function:

init: function() {
	this._setDefaultModelForGroup();

Unfortunately this has no effect of the default model. Only the metadata is loaded, but the default model isn't replaces.

Hope someone could jump in here with some help.

Best regards
Gregor