Pieter
Pieter

Reputation: 1831

SAPUI5 > 1.30, how to set custom model headers before initial request

Before the use of a manifest file I used to add custom headers to the config of my oData model. Now starting from SAPUI5 1.30 the Component.js is using a manifest file and as soon as the runtime enters the Component.js init() function the model is already loaded and a first request is already made to my service. I need a way to set my custom headers at runtime and before the first request to my service is made.

Before:

    // The service URL for the oData model
    var oServiceConfig = this.getMetadata().getConfig().serviceConfig;
    var sServiceUrl = oServiceConfig.serviceUrl;

    // the metadata is read to get the location of the i18n language files later
    var mConfig = this.getMetadata().getConfig();
    this._routeMatchedHandler = new sap.m.routing.RouteMatchedHandler(this.getRouter(), this._bRouterCloseDialogs);

    // create oData model
    this._initODataModel(sServiceUrl);

// _initODataModel function

    headers = {custom: 'hello world'};
    var oConfig = {
        metadataUrlParams: {},
        json: true,
        // loadMetadataAsync : true,
        defaultBindingMode: "OneWay",
        defaultCountMode: "Inline",
        useBatch: true,
        headers: headers
    };

    var oModel = new sap.ui.model.odata.ODataModel(sServiceUrl, oConfig);       
    this.setModel(oModel);

Upvotes: 1

Views: 4898

Answers (2)

Nabi
Nabi

Reputation: 2566

The manifest.json file aka app descriptor is not as dynamic as you would need it. In other words: you can't achieve what you want using the app descriptor. This is a drawback of using the app descriptor.

You could use your Component.js instead to have something dynamic. In there you could instantiate you model manually... Maybe you could also configure the component the "old" way without the manifest.json file.

Upvotes: 1

schnoedel
schnoedel

Reputation: 3948

you can specify the object that is passed to the models constructor in the manifest.json in sap.ui5/models/myModel/settings:

"sap.app": {
  "dataSources": {
    "myDatasource": {
      "uri": "/sap/hba/r/apf/core/odata/apf.xsodata",
      "type": "OData",
      "settings": {
         "odataVersion": "2.0"
      }
    }
  }
},
"sap.ui5": {
  "models": {
     "myModel": {
        "datasource": "myDatasource",
        "settings": {
          "headers": {
             "headername1":"headervalue1",
             "headername2":"headervalue2",
          }
        }
     }
  }  
}

Upvotes: 0

Related Questions