cancel
Showing results for 
Search instead for 
Did you mean: 

UI5 Binding to global model

gary_king2
Participant
0 Kudos

At present we have a global model (GM) defined in our /model area and then loaded via the manifest. This model is available via the XML view and using .getModel/.getProperty is also available to the view controller as well.

Out current practice is to get the required data from the global model for a specific property path (GM>/contacts/Insurance) , and then create a model from that and bind it to view table control, by ID. The view table then has access to the data in the global model. However, I believe this is the wrong approach, as I don't think it will update the global model (two way) when we Insert/delete rows in the View table, due to the way we have bound a new model to the view table, or am I incorrect?.

Ideally, What I would want to do is hard code the XML view control like so:

<Table items="{GM>/contacts/Insurance}" ...

Where GM is out global model, and the View table points to the array /contacts/Insurance of the json model.

Within the view table control we use buttons to call functions when we need to Insert/Delete rows. As the XML view has the binding hard-coded to "{GM>/contacts/Insurance"} how can I get back the binding being used, without physically having it hard-coded in the controller functions?. Is that possible.

Accepted Solutions (0)

Answers (2)

Answers (2)

WouterLemaire
Active Contributor
0 Kudos

Hi,

Do I understand you correct and are you using the same global model in your table in the view? Then it should just work. But you'll have to active TwoWay binding in the manifest.json.

Kind regards,

Wouter

gary_king2
Participant
0 Kudos

Two-way binding is the default for a json model, I believe.

As I have just mentioned to Jun, my problem is not in binding my View table to a global mode, or updating it, the problem is how I can get my controller Insert/delete functions to work so that they can insert/delete rows from my view table. I have given code examples in my reply to Jun.

junwu
Active Contributor
0 Kudos

have u done anything?

just bind to the path, nothing else. if you are using jsonmodel

gary_king2
Participant
0 Kudos

Do you have an example you can point to.

As mentioned I have no problems binding my fields and tables to the JSON model. Where I have the issue is when I need to Insert/delete from a view table that is bound.

Assuming I have a JSON model where the data in the table needs to point to the global model "GM>/contacts/insurance"

In my XML I can have <Table Items="{GM>/contacts/insurance}" ...

But when my functions to insert/delete rows in this table are called, how should I determine the model/data?.

For example, if I had a local model rather than a Global model, defined in the controller, I could have the code bound to the table like so:

var oMyData = [];
var myModel= new sap.ui.model.json.JSONModel(oMyData);
this.getView().byId("idTblMyTable").setModel(myModel, "aliasModel");

And then my function to insert a new row into the table would look like this:

onAddNewRow: function(oEvent) { 
	var newRecord = {};
	var oTableData = this.getView().byId("idTblMytable").getModel("aliasModel").getData();
      oTableData.push(newRecord);
      this.getView().byId("idTblMytable").getModel("aliasModel").setData(oTableData);
      this.getView().byId("idTblMytable").getModel("aliasModel").refresh();
}

Now I know that this code is NOT necessary to bind the global JSON model to a table because I can directly reference the model within my XML like so:

<Table Items="GM>/contacts/insurance}" .... 

If I do this, then what should I replace the 2nd line in my onAddNewRow function to so that it can access the data?.

Do I need to try it this way?

onAddNewRow: function(oEvent) {
    var newRecord = {};
    var lgm = this.getOwnerComponent().getModel("GM");
    lgm.getProperty("/contacts/insurance",oTableData);
    oTableData.push(newRecord);
 }

Getting the exact code to successfully update the model is what I'm having trouble with.

junwu
Active Contributor
0 Kudos
no need local model, just bind to global one.
 var lgm = this.getOwnerComponent().getModel("GM");

var tabledata=lgm.getProperty("/contacts/insurance");
tabledata.push(newrecord);

lgm.setProperty("/contacts/insurance",tabledata);