Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

How to set / change "serviceUrlParams" in default model at runtime?

Oliver_Baer
Explorer
0 Kudos

Dear Experts,

I want to set or change the "serviceUrlParams" in the default model which is generated automatically from the information in the "manifest.json".

The following link refers to an older post which shows a solution, but the solution does not work in my case: https://archive.sap.com/discussions/thread/3903730

I added the following code (as described in the post) to the function "init" in the "Component.js", but this has no effect, because the default model "already exists":

var oManifestUi5 = this.getMetadata().getManifestEntry("sap.ui5");
oManifestUi5.models[""].settings.serviceUrlParams["sap-client"] = _sapClient;

The default model must have been created "before" the function "init", so it already exists and I can call

this.getModel();

to get a reference to the model and change it, the problem is that "serviceUrlParams" is a private member and can't be changed at runtime.

Does anyone has a solution?

Best regards, Oliver

  • SAP Managed Tags:
1 ACCEPTED SOLUTION

adrian_zguri
Explorer
0 Kudos

Hi Oliver,

I found a workaround for that.

In your Component.js you should override the function "initComponentModels".

In my case I needed a Metadata Hash parameter to know if I needed to retrieve the metadata from the Cache or the Server.

Here is my code.

/**
         * It was copied like it is but then added the section "manifest.json"
         * 
         * @override
         */ 
        initComponentModels: function() {
            // in case of having no parent metadata we simply skip that function
            // since this would mean to init the models on the Component base class
            var oMetadata = this.getMetadata();
            if (oMetadata.isBaseClass()) {
                return;
            }
    
            // retrieve the merged sap.app and sap.ui5 sections of the manifest
            // to create the models for the component + inherited ones
            var oManifestDataSources = this._getManifestEntry("/sap.app/dataSources", true) || {};
            var oManifestModels = this._getManifestEntry("/sap.ui5/models", true) || {};
            
/*********** StartOf manifest.json ****************************************************/
            var mtHash = jQuery.sap.getUriParameters().get("metadata-hash");
            if (mtHash) {
                oManifestModels[""].settings.metadataUrlParams.hash = mtHash;
            }
/********** EndOf manifest.json ******************************************************/
            
            // pass the models and data sources to the internal helper
            this._initComponentModels(oManifestModels, oManifestDataSources);
        }

If you just need to change the parameter "sap-client" though, this is not the right way. (In my previous post I was inexperienced and didn't know how things work). You can change the parameter "sap-client" as a Url Parameter.

So in case your website is http://my.website.com you jsut write "http://my.website.com?sap-client=100"

I hope this helps.

Best regards

Adrian

  • SAP Managed Tags:
3 REPLIES 3

adrian_zguri
Explorer
0 Kudos

Hi Oliver,

I found a workaround for that.

In your Component.js you should override the function "initComponentModels".

In my case I needed a Metadata Hash parameter to know if I needed to retrieve the metadata from the Cache or the Server.

Here is my code.

/**
         * It was copied like it is but then added the section "manifest.json"
         * 
         * @override
         */ 
        initComponentModels: function() {
            // in case of having no parent metadata we simply skip that function
            // since this would mean to init the models on the Component base class
            var oMetadata = this.getMetadata();
            if (oMetadata.isBaseClass()) {
                return;
            }
    
            // retrieve the merged sap.app and sap.ui5 sections of the manifest
            // to create the models for the component + inherited ones
            var oManifestDataSources = this._getManifestEntry("/sap.app/dataSources", true) || {};
            var oManifestModels = this._getManifestEntry("/sap.ui5/models", true) || {};
            
/*********** StartOf manifest.json ****************************************************/
            var mtHash = jQuery.sap.getUriParameters().get("metadata-hash");
            if (mtHash) {
                oManifestModels[""].settings.metadataUrlParams.hash = mtHash;
            }
/********** EndOf manifest.json ******************************************************/
            
            // pass the models and data sources to the internal helper
            this._initComponentModels(oManifestModels, oManifestDataSources);
        }

If you just need to change the parameter "sap-client" though, this is not the right way. (In my previous post I was inexperienced and didn't know how things work). You can change the parameter "sap-client" as a Url Parameter.

So in case your website is http://my.website.com you jsut write "http://my.website.com?sap-client=100"

I hope this helps.

Best regards

Adrian

  • SAP Managed Tags:

Oliver_Baer
Explorer

Hi Adrian,

your solution works, but I don't know if it is the best way to override the function "initComponentModels" because when the function changes in a newer SAPUI5-Version this could be a problem.

I found another solution that works for me: you can set a custom header on the model in the Components "init"-function at runtime:

init: function() {
// call the base component's init function UIComponent.prototype.init.apply(this, arguments);
//... var oComponentData = this.getComponentData(); if (oComponentData) { var test = oComponentData.startupParameters["test"]; this.getModel().setHeaders({"X-TEST": test}); }
}

In the ABAP-Gateway-System I can read this custom header:

DATA: lt_header      TYPE tihttpnvp,
      ls_header      LIKE LINE OF lt_header,
      l_test         TYPE string,
      lo_request     TYPE REF TO /iwbep/cl_mgw_request,
      lo_req_details TYPE /iwbep/if_mgw_core_srv_runtime=>ty_s_mgw_request_context.

REFRESH lt_header.
lo_request ?= i_o_tech_request_context.
lo_req_details = lo_request->get_request_details( ).
APPEND LINES OF lo_req_details-technical_request-request_header TO lt_header.

* Read parameters (lower-case!) from HTTP-Header
READ TABLE lt_header INTO ls_header WITH KEY name = 'x-test'.
IF sy-subrc = 0.
  l_test = ls_header-value.
  CONDENSE l_test.
ENDIF.

If anyone has a better solution please let me know.

Oliver

  • SAP Managed Tags:

0 Kudos

Great! Thank you.

  • SAP Managed Tags: