cancel
Showing results for 
Search instead for 
Did you mean: 

setting Model to sap.ui.getCore not working

Former Member
0 Kudos

Hi All,
In controller init event I have


var myJSON = {"title":"This is Test"}

oModel.setData(myJSON);

sap.ui.getCore().setModel(oModel, "MyJSONData")

In view Create Event I have


var controls = []

controls.push(new sap.ui.commons.Label({

                    text:{

                              parts:[{path:"MYJSONData>/title"}],

                              formatter: function(title)

                                   {

                                        //will do some formatting and return formatted title

                                     }

                           }

               }));

but it is not binding
But when i changes controller code
as

this.getView().setModel(oModel,"MyJSONData")

It binds data(title) to View.

Question is when I set model to UI5 core object , why it never binds data but when I set model to view it binds the data.
I have several nested views within one parent view and I want same model to be used across all views. I don't want to set separate model for each view.

Any help is appreciated ,

Thanks in advance.

Accepted Solutions (0)

Answers (6)

Answers (6)

Former Member
0 Kudos

Hi Sanket,

Create a parent controller class and implement there the logic common for all other controllers.

In your case it is probably:


onInit : function () {

     var myJSON = {"title":"This is Test"}

     oModel.setData(myJSON);

     sap.ui.getCore().setModel(oModel, "MyJSONData")

}

Then in sub-classes use the following:


onInit : function () {

     MyGenericController.prototype.onInit.apply(this, arguments);

}

Former Member
0 Kudos

Hi Sanket,

     I also face the same problem when I converted my app to new sap standards (while adding component.js and manifest.json). I think we cannot set the model name at core level may be because of model name conflict with other app model name.

     So the solution is we can set model name with the view.

ex:

     var myJSON = {"title":"This is Test"

     oModel.setData(myJSON); 

     // sap.ui.getCore().setModel(oModel, "MyJSONData"); // this will not work while binding

    this.getView().setModel(oModel, "MyJSONData");  // this will work

similarly you get that model

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

martin_zaman
Explorer
0 Kudos

Hi together,

i have the very same problem.

sap.ui.getCore().getModel(...) is empty in view!

getView().getModel() is filled with data.

i can't access data from Core model.

i created a shell so that views are only loaded if needed.

Model registration in Component.js init: function()


jQuery.sap.registerModulePath('model.ModelTaskHandler','./model/ModelTaskHandler');

jQuery.sap.require("model.ModelTaskHandler");

sap.ui.getCore().registerPlugin(new model.ModelTaskHandler('model.ModelTaskHandler'));

The model is implemented in other js object. Data load is external. But the data is correct and available.

View:


projektnameTF = new sap.ui.commons.TextField( {

      value : '{task>/projektname}',

      editable : false,

      width : "100%"

});

If im setting the core model to the view model, it works and data load is successful!


taskmodelReadyHandler : function(channelId, eventId, data) {

       this.getView().setModel(sap.ui.getCore().getModel(task), 'task');

}

martin_zaman
Explorer
0 Kudos

I think reason is the container.

There is no access to global model from a "sap.ui.core.Control.extend".


Now i created the shell in an App.

kedarT
Active Contributor
0 Kudos

Hi Sanket,


sap.ui.getCore().setModel(oModel, "MyJSONData")  make this model known to the framework.


Control Label is embedded in the control View and it is the Parent Control for the Label. Binding for Control Label shall not be known unless the View control is not bound the model instance.


More information can be found in the below link:

SAPUI5 SDK - Demo Kit


Hope this helps you.

Former Member
0 Kudos

Hi Kedar,

I agree with you. But question arises that, why control always bind to view Model not to global model that we set using sap.ui.getCore().setModel(myModel, "modelName").

Up till now, I am  assured that, view control will not get bind unless you set model to view or that control it self.
But It is my assumption that if model is not set to view/control then control should look global model and then bind data base on path.
Or
If control is not able to find property(path) in view/control Model then it should search in global Model for same path.

former_member182862
Active Contributor
0 Kudos

Hi Sanket

I am pretty sure that it works.

Example

It is does not work in your case, then it has to be something else. You will likely to find the answer if you start to strip down the code around the label and check when it starts to work.

-D

kedarT
Active Contributor
0 Kudos

Hi Sanket,

I may not be quoted on this but will try to make a logical sense.

Within a View there could be many controls and those controls would be bound to different entitysets of the Model. Hence you will have to bind the control explicitly to the Entityset by mentioning its path in the model and bind the view to just the model.

Or the other option would be to bind the model to the view and mention the path of the entityset while binding the property of the controls(something like what Dennis has done in his example).

Hope this answers your question.

Former Member
0 Kudos

Actually it just cant be exactly figured out why this doesn't work for you unless you provide a snippet replicating the issue.

Some of the possible reasons could be,

  • You're making a typo. (model set as -> MyJSONData & you're accessing it as MYJSONData as per the question)             
  • missing out data-sap-ui-xx-bindingSyntax="complex" in the bootstrap script. (but missing this shouldn't work for both the cases)
  • somewhere in the code you're setting the model MyJSONData to the core again and it gets overridden with new data.

You can also try opening your console and execute sap.ui.getCore().getModel('your-model-name') to retrieve your model and find what data does the model hold.

Former Member
0 Kudos

Hi Elango,

I have check in my code, there is not typo mistake. your 2nd point is not valid as I am able to bind when I set model to view. On 3rd point, I have check in chorme console, data is set properly to model ,

sap.ui.getCore().getModel("MyJSON") is returning proper value, it is not overridden by any other model.

padma_mundru
Participant
0 Kudos

Hi Sanket,

please follow the below url to know the DataBinding

Data Binding: Getting Started - User Interface Add-On for SAP NetWeaver - SAP Library




Create and set the model in index.html for sharing data to several nested views.




Thanks & Regards

Padma Mundru.

Former Member
0 Kudos

Hi padma,

I didn't get difference between setting model in index.html and in controller init event.  Even after setting model in index.html page I am not able to view title in page.

I have already gone through the link that you have posted. Many other places I am able to bind data. but I didnt get diff why when I set Model to view it binds data and when I set model to Core() its not biding.

padma_mundru
Participant
0 Kudos

Hi Sanket,

Creating Global JSON Model in SAPUI5 Project

We will now see how to use JSON model that is globally accessible to all views of your SAPUI5 project. We will define a JSON model in Index HTML instead of a view of our application. Following code defines JSON model and sets the data to be stored when application is initially loaded. We will modify this default data by accepting user data at the runtime.

1

2

3

4

5

6

7

8

9

10

11

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

oModel.setData({

firstName: "Enter First Name",

lastName: "Enter Last Name"

});

sap.ui.getCore().setModel(oModel);

Thanks&Regards
Padma mundru
Former Member
0 Kudos

Hi Padma,

I think you are not getting my problem.
I have set model with some name, in my case it is "MYJSONData"

so when I write

  1. sap.ui.getCore().setModel(oModel, "MyJSONData"//it is not binding data to label

but when I write

  1. this.getView().setModel(oModel, "MyJSONData"// It is binding data to label


I have binded as



  1. controls.push(new sap.ui.commons.Label({ 
  2.                     text:{ 
  3.                               parts:[{path:"MYJSONData>/title"}], 
  4.                               formatter: function(title) 
  5.                                    { 
  6.                                         //will do some formatting and return formatted title 
  7.                                      } 
  8.                            } 
  9.                })); 


padma_mundru
Participant
0 Kudos

Hi Sanket

if you want to bind the data through the core try as follows

get the view id from "index.html or from your defined view " by using

"sap.ui.getCore().byId("viewid"); and the n set it to the model.

Please let me know if it not works

Thanks&Regards

Padma Mundru

Former Member
0 Kudos

Hi Padma,

Again same thing. I am able to bind data when I set model to View but when I set model to core it is not binding. I don't want to set model to view. I just pass path with model name while bind to label. why it is not picking up Model path ("title") to bind label when model is set to sap.ui.getCore().setModel, but work when I set model to this.getView().setModel

sudarshan_d29
Active Participant
0 Kudos

Hi Sanket,

why first line is not working, secound line is working?


Because of, while calling method getView() upload the page detail and then set the model for the page on it by using setModel().


Hope answer is ok.


regards,

sudarshan