cancel
Showing results for 
Search instead for 
Did you mean: 

Set values of model (automatic model instantiation)

Former Member
0 Kudos

Hey,

I have a Fiori App where the Model is loaded via automatic model instantiation.

Is there any possibility add parameters in the init function of the Component? (or somewhere else)

I want to add header values, but I dont want to add them in the manifest.json cause of security aspect.

Thank you very much for your help.

Best regards,

David

Accepted Solutions (0)

Answers (1)

Answers (1)

SergioG_TX
Active Contributor
0 Kudos

David,

ideally you want to keep your named models listed in the manifest file, however, you can also create a named model in the component as well inside the init function and then assigning it to the view.

Former Member
0 Kudos

But when I instantiate my model via automatic model instantiation ( SAPUI5 SDK - Demo Kit ) I dont have any name of the default model?!

Or is there any other way to change values of the default model, that I can set the request headers?

Thanks

David

SergioG_TX
Active Contributor
0 Kudos

that is also possible.. you can have 1 unnamed model.. and many named models..

if i understand correctly, you are wanting to update properties of the model (unnamed or named)

within a controller, you can use the following line of code:

var unnamedModel = this.getView().getModel();

var namedModel = this.getView().getModel('modelName');


to read a property:

var propertyValue = model.oData.propertyName;


to update a property:

model.setProperty('/pathTo/property', propValue)


hope this is what you were looking for

Former Member
0 Kudos

Hey,

I dont wanna change the properties of a model. I wanna set the parameters of a model. Espacially the authorization header for instantiating.

I've tested it with values in the manifest.json under the default model. But is it also possible to set these values in the init Component method, before any model is instantiated?

Thanks for your help,

David

SergioG_TX
Active Contributor
0 Kudos

check this out.. parameters on odata models and custom headers

SAPUI5 SDK - Demo Kit

Former Member
0 Kudos

Yes I already read this. But in this cases all the models get instantiated manualley. But in my app the default model gets instantiated automatically. So I need to access the default model and change its headers.

Sorry I'm very new to SAPUI5.

SergioG_TX
Active Contributor
0 Kudos

so would this work...

1) get a handle of your model

var model = this.getView().getModel(); // named or unnamed whichever you need

2) add custom headers

model.setHeaders(  ); // pass an object with the headers you need

model.refresh();  // self explanatory

what do you need to do after here?  hope this is what you need.

Don't apologize... welcome to the world of sapui5   many of us learn each day and that is a great thing

Former Member
0 Kudos

So I have to put this code in the App Controller? Because I dont have a view in the Component. Am I right?

SergioG_TX
Active Contributor
0 Kudos

correct, you do not have a view on the component, however, you can still set it on the Component .

maybe someone else may have a different opinion ?  try it and see ...

also, you may need to see what the context of the   this   keyword is on your component... the this keyword sometimes is out of context so make sure you have the component context for it.

good luck!