Skip to Content
-2

UI5 Binding to global model

Apr 04, 2017 at 01:52 PM

158

avatar image

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.

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

2 Answers

Jun Wu Apr 04, 2017 at 02:19 PM
0

have u done anything?

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

Show 2 Share
10 |10000 characters needed characters left characters exceeded

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.

0
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);
0
Wouter Lemaire
Apr 04, 2017 at 06:51 PM
0

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

Show 1 Share
10 |10000 characters needed characters left characters exceeded

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.

0