Vinny
Vinny

Reputation: 865

Loading a model with Name in SAPUI5

I am trying to load models into a view. i am able to load the model when i do not give any name for the model like:-

sap.ui.getCore().byId("mao").setModel(oModel);

But the model is not set to view if i give like the below. I am not getting any error also

sap.ui.getCore().byId("mao").setModel(oModel,"myModel");

I am using load data to load my data

    var oModel = new sap.ui.model.json.JSONModel();
    oModel.loadData(servicePath);
    sap.ui.getCore().byId("mao").setModel(oModel);

Upvotes: 3

Views: 4956

Answers (5)

Krishna Prasad D
Krishna Prasad D

Reputation: 41

this._sValidPath =jQuery.sap.getModulePath("OpenUI.c", "/name.pdf"); this._oModel = new JSONModel({ Source: this._sValidPath, Title: "My Custom Title", Height: "600px" }); sap.ui.getCore().setModel(this._oModel,"mainModel2");

Upvotes: 0

neeko
neeko

Reputation: 2000

Try this:

var oModel = new sap.ui.model.json.JSONModel();
oModel.loadData(servicePath);

var oController = this;

oModel.attachRequestCompleted(function() {
     var data = oModel.getData();
     oController.getView().setModel(data, "namedModel");
});

Then you can call like so:

this.getView().getModel("namedModel");

Upvotes: 2

Vijayendra Gade
Vijayendra Gade

Reputation: 79

The best way to set a model to a view is by defining them in Component.js file or if you are using 1.33 and above version then define the model in manifest.json file.

  • Set Model in Component.js file: Define the model init method as shown below:

    sap.ui.define(['jquery.sap.global', 'sap/ui/core/UIComponent'],
    function(jQuery, UIComponent) {
    "use strict";
    
    var Component = UIComponent.extend("samples.components.sample.Component", {
    metadata : {
    }
    init: function() {
       var sServiceUrl = 'example/servie';
       var oModel = new sap.ui.model.odata.ODataModel (sServiceUrl, true);
       this.setModel (oModel, "modelName");
    }
    });
    return Component;
    });
    
  • Set the model in manifest.json file https://openui5.hana.ondemand.com/#docs/guide/be0cf40f61184b358b5faedaec98b2da.html

Now this model will be available in any controller and you can access the model as mentioned below:

  var oModel = this.getView().getModel("modelName");

Upvotes: 0

Beka
Beka

Reputation: 343

sap.ui.getCore().byId("mao").setModel(oModel);

But the model is not set to view if i give like the below.

You did not bind this to the View you bound it to the core. Remove the .byId("mao") and it should work.

var oModel = new sap.ui.model.json.JSONModel();
oModel.loadData(servicePath);
sap.ui.getCore().setModel(oModel, "myModelName");

Read Model:

var oMyModel = sap.ui.getCore().getModel("myModelName");

sap.ui.getCore().byId("mao").setModel("myModel") means you want to get a object by the ID "mao" and assign a model to the object.

This is how the view binding works:

this.getView().setModel(oModel, "myModelName");
var oMyModel = this.getView().getModel("myModelName");

On the OpenUI5 site theres a nice developer walkthrough check this out: https://openui5beta.hana.ondemand.com/#docs/guide/70ef981d350a495b940640801701c409.html

Upvotes: -1

Haojie
Haojie

Reputation: 5713

Your binding paths in the view should also be adjusted for named model binding. Details please see at documentaion regarding the binding path.

Upvotes: 0

Related Questions