UI5: Setting a different model within a view

Home / Blog / UI5: Setting a different model within a view

Here was my scenario: I had a UI5 table that called a model.  The model had a list of notifications along with some additional information.  If you click on one of the lines, it would bring up another view, that showed all the details of the model.  The OData service for the details was a different call than for the table.  So for the new view, I needed to set a new model.  I was able to pull the data into my view within the components, but making it available on the XML was another story.  Here is what I finally did:

  1. On the detail view I added the following to a routine that gets called every visit to the view (do not put this in OnInit, or it will only be called the first time).
    1. var oViewModel = sap.ui.getCore().getModel(“notdet”);
    2. this.setModel(oViewModel);
  2. Then in the XML, I was able to use the following syntax:  {/results/0/NotifNo}
    1. for me, it was treated like an array with only one value coming back.  so I explicitly set it to 0.

I hope this can save some headaches for someone else.  I won’t soon be forgetting this.

Thanks for reading,

As always, thanks for reading and don't forget to check out our SAP Service Management Products at my other company JaveLLin Solutions,
Mike

Leave a Reply

Your email address will not be published. Required fields are marked *