cancel
Showing results for 
Search instead for 
Did you mean: 

binding of text field vs setvalue of text field from model

amber_garg
Active Participant
0 Kudos

Hi ,

I have a json model having an array of records. In my ui5 layout i just have a set of text fields for each of the fields defined in the json model. I figured out that there are two ways by which i can read the particular index of the json array from model and bring it to the text field.

1) Using binding


var oJson = new sap.ui.model.json.JSONModel();

   oJson.loadData("Data/empdata.json","",false);

   sap.ui.getCore().setModel(oJson);

   oTF_Empno.bindValue("/Empl_Data/"+emp_index+"/Empno");

2) Using setValue instead of binding


var oJson = new sap.ui.model.json.JSONModel();

   oJson.loadData("Data/empdata.json","",false);

   sap.ui.getCore().setModel(oJson);

   sap.ui.getCore().byId("Empno").setValue(sap.ui.getCore().getModel().getData().Empl_Data[emp_index].Empno);

Both the methods are working but I am confused regarding the difference and usefulness of them. Also from performance point of view , which of the above methods is advisible to use for different scenarios

Regards

Amber

Accepted Solutions (1)

Accepted Solutions (1)

former_member182862
Active Contributor
0 Kudos

HI Amber

In one case, you are binding property to value of the control. In the other case, you are setting the value. In the former case, the value of the control will change when the value in the model change.

Here is a sample to illustrate: JS Bin - Collaborative JavaScript Debugging

Thanks

-D

former_member182862
Active Contributor
0 Kudos

Sorry I missed your question about performance.

for model binding, it is more involved hence there are performance impact but this impact is insignificant in most of the cases.

-D

amber_garg
Active Participant
0 Kudos

Thanks for your reply Dennis.I checked that JS Bin code. "In the former case, the value of the control will change when the value in the model change." , and vice versa won't be true , right?? that is (if value is changed in control by user , it wont change it on model)?

If the above is true and suppose if the scenario is I bring the data from json file to model , then (its unlikely that the source data in the model is going to change during the time user is viewing the webpage) , would'nt it be better from performance perspective to use SetValue instead of binding?? Saw your reply on performannce after this post so editing

Kindly suggest me on this as in the JS Bin code , i am able to get the same functionality for "static" field too using setValue hence I am still unable to get a scenario where binding wins over setValue.

Thanks

Amber

former_member182862
Active Contributor
0 Kudos

Hi Amber

the binding is a two ways binding. That's whenever the value in the input control changes that model will change too. this is one of the most powerful feature in SAPUI5. A very comprehensive model and control binding mechanism

To help you understand this better, we have another sample

JS Bin - Collaborative JavaScript Debugging

Change the value in the first input and click on the Check Model button. you can see that the model value is changed accordingly.

But change the value in the second input and click on the Check Model button. you can see that the model value remains unchanged.

Thanks

-D

amber_garg
Active Participant
0 Kudos

Hi Dennis ,

Thanks. I understood the importance of 2 way binding.

Now comparing 1 way binding  with SetValue , is there any specific advantage to go with binding method as a case would be very rare where a model will get changed after i have set the model in the application..

Thanks

Amber

former_member182862
Active Contributor
0 Kudos

HI Amber

Among other things, the main advantage is to simplify code development and maintenance. Once the controls are bind to the property in the model, we no longer need to hunt down the control by its ID and setValue or reset Value.

JS Bin - Collaborative JavaScript Debugging

This is new sample, we can use binding to control the visibility of icon and have one than one control having the same values.

When we reset the value in the model, all the corresponding values in the controls changes all together.

Thanks

-D

amber_garg
Active Participant
0 Kudos

Thanks a lot Dennis for your detailed examples with scenarios

former_member182862
Active Contributor
0 Kudos

You're welcome, Amber

There are many hidden things in SAPUI5 and I believe that the only way to discover them is via code sample and discussions like this one. Questions may be simple like this one. However, understand them help us to design and architect our code.

-D

amber_garg
Active Participant
0 Kudos

Yes, thats 100% true

Answers (0)