Bind JSON to view (SAPUI5) with model name

21,303

It is possible to bind n numbers of models to the view. You can specify the alias with which model is bound to the view ( or any control). You do not specify alias while creating model but at the time of setting the model to the control ( like view).

So, this is wrong:

var data = {name : "name"};
var oModel = new JSONModel(data, "data"); // Alias is not specified here
this.getView().setModel(oModel);

Correct code is :

var data = {name : "name"};
var oModel = new JSONModel(data); // Only set data here.
this.getView().setModel(oModel, "data"); // set the alias here

Similarly,

var data = {employeeName: "Rahul"};
var oModel = new JSONModel(data); // Only set data here.
this.getView().setModel(oModel, "EmployeeData"); // set the alias here

And in View,

<m:Input id="name" value="{data>/name}" enabled="false" description="{EmployeeData>/employeeName}"/>
<m:Input id="name" value="{EmployeeData>/employeeName}" enabled="false"/>
Share:
21,303
Albertus
Author by

Albertus

Updated on January 03, 2020

Comments

  • Albertus
    Albertus over 4 years

    If I want to bind JSON to view:

    XML:

    <m:Input id="name" value="{/name}" enabled="false"/>
    

    controller:

    var data = {name : "name"};
    var oModel = new JSONModel(data);
    this.getView().setModel(oModel);
    

    and how to bind JSON to view with spesified model name?

    controller :

    var data = {name : "name"};
    var oModel = new JSONModel(data, "data");
    this.getView().setModel(oModel);
    

    how to code in xml? I try this but not bind.

    <m:Input id="name" value="{data>/name}" enabled="false"/>
    

    because I want to bind with 2 source json data, if i set with spesified model name, just one model bind to view.

    Thanks, Bobby

  • Albertus
    Albertus over 7 years
    I should try this but not bind.. I try with this to table or other component, no problem. thanks
  • JimiOr2
    JimiOr2 over 6 years
    Now I could bind with model name thanks. But another question is when I use List control , how do I bind value on the StandListItem? Can't display title and id on items. e.g <List id="dialogListMpn" headerText="MPN" items="{onDialogMpnSearch>/albums}"> <StandardListItem title="{title}" description="{id}" type="Active" press="onMpnItemPress"/> </List>
  • Rahul Bhardwaj
    Rahul Bhardwaj over 6 years
    You will have to use the Model alias name which you bound your List to. ie. <List id="dialogListMpn" headerText="MPN" items="{onDialogMpnSearch>/albums}"> <StandardListItem title="{onDialogMpnSearch>title}" description="{onDialogMpnSearch>id}" type="Active" press="onMpnItemPress"/> </List>. Model name has to be specified for every binding to that model. If you don't specify, it will try to fetch from un-named model.