Skip to Content

Multi Model Support and Binding For OData Services

Problem:

Many times we need to include an OData service which is common for many applications.In this scenario instead re writing the OData

service again we should create more than one model.

Create Main OData Model and set it globally :


this.metadataUrl = "proxy/sap/opu/odata/sap/ZMM_MOBILE_MATERIAL_SRV/";   sap.ca.common.uilib.Ajax.registerModelUrl(
  "sap/opu/odata/sap/ZMM_MOBILE_MATERIAL_SRV/",
  "proxy/sap/opu/odata/sap/ZMM_MOBILE_MATERIAL_SRV/");
this.oDataModel = new sap.ui.model.odata.ODataModel(this.metadataUrl,true);
this.oDataModel.setCountSupported(false);
sap.ui.getCore().setModel(this.oDataModel,"mainModel");




Create Second OData Model :


this.metadataUrl2 = "proxy/sap/opu/odata/sap/Z_NEW_ODATA_SRV/";   sap.ca.common.uilib.Ajax.registerModelUrl(
  "sap/opu/odata/sap/Z_NEW_ODATA_SRV/",
  "proxy/sap/opu/odata/sap/Z_NEW_ODATA_SRV/");
this.oDataModel2 = new sap.ui.model.odata.ODataModel(this.metadataUrl2,true);
this.oDataModel2.setCountSupported(false);
sap.ui.getCore().setModel(this.oDataModel2,"secondModel");




Explanation:


sap.ui.getCore().setModel(this.oDataModel,"mainModel");
sap.ui.getCore().setModel(this.oDataModel,"secondModel");




The above line will set OData models to the core of the application.To distinguish between both of them in our application we can give

modelNames(mainModel,secondModel) while setting it.

Using The Models:

Say you want to set the “secondModel” to a list control :


this.getView().byId("list").setModel(sap.ui.getCore().getModel("secondModel"));




Property Binding :


<Text text="{mainModel>/name}"/>
<Text text="{secondModel>/name}"/>




Regards,

Rauf

1 Comment
You must be Logged on to comment or reply to a post.
  • Hello Rauf,

    how can I use this coding for Fiori-extension?

    Where should I define the second model in the extension project? in Component.js?

    How can I manage Data, if the view in main project should call the special sub-view from the extension and where should be displayed the data from another Model?

    Thanks in advance.

    Regards,

    Dmitry