on 09-20-2015 6:53 PM
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
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
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
User | Count |
---|---|
85 | |
10 | |
10 | |
10 | |
7 | |
6 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.