cancel
Showing results for 
Search instead for 
Did you mean: 

Access value of my oData model

Former Member
0 Kudos

Hi there,

I've created a form which fields are bound to a oData model.

That works fine.

But now I've the following problem:

I have a value RequestTypeID in my model which should not be displayed on the view, but which should be used to determine how data should be displayed on the view.

How can I access that value?

I've tried the following:


var oTextRequestTypeID = new sap.ui.commons.TextField(

     "textRequestTypeID",

     {

          value : "{HandleRequestType/Request/RequestTypeID}"

     });

var requestTypeID = "";

requestTypeID = oTextRequestTypeID.getValue();

Nevertheless my variable requestTypeID still keeps the value "".

But if I display the TextField oTextRequestTypeID on the screen, it displays the correct value.

My question:

What is the best practice to access such values which should not be displayed on the view?

Is it possible to bind it directly to a variable?

Thanks in advance.

Best regards,

Thorsten.

Accepted Solutions (1)

Accepted Solutions (1)

ChandraMahajan
Active Contributor
0 Kudos

Hi,

you can use getProperty() method. Refer SAPUI5 SDK - Demo Kit

Also refer this document for more information.

Regards,

Chandra

Former Member
0 Kudos

Hi Chandra,

I've tried now the getProperty() method, but unfortunately is doesn't work.

First I create my model for the BPM oData service:


// Get taskID from URL

var taskId = getValueOfURLParameter("taskId");

// Create ODataModel for BPM Task Data OData service

var taskDataSvcURL = "/bpmodata/taskdata.svc/" + taskId;

var oDataModel = new sap.ui.model.odata.ODataModel(taskDataSvcURL, true);

Afterwards I'm trying to bind a property to a variable:


var RequestTypeID = oDataModel.getProperty("/InputData('"+ taskId +

                                             "')/HandleRequestType/Request/RequestTypeID");

But unfortunately RequestTypeID is still undefined.

Even if I put sap.ui.getCore().getModel().getData() into the console, it returns a null value.

Qualiture
Active Contributor
0 Kudos

Because of the asynchronous nature of getting the OData data, you could only get the data once it's loaded. Ideally, you would do so in the 'requestCompleted' event handler:

    oDataModel.attachRequestCompleted(function() {

        var prop = oDataModel.getProperty("/yourproperty");

        // etc

    })

Former Member
0 Kudos

Thanks, Robin!

I've tried to use attachRequestCompleted(), but unfortunately it's never called:


createContent : function(oController) {

     // Create model

     oDataModel = oController.createModel();

     oDataModel.attachRequestCompleted(function() {

          var requestTypeID = oDataModel.getProperty("/InputData('"

               + taskId + "')/HandleRequestType/Request/RequestID");

       

          this.buildForm(oController, requestTypeID);

     });

  }

Qualiture
Active Contributor
0 Kudos

That's weird... can you maybe implement the attachRequestSent and attachRequestFailed event handlers for your model and see if they got called (and hopefully see where it went wrong)

Former Member
0 Kudos

Hi Robin,

I've added the other two methods, but they're not called either:


createContent : function(oController) {

     // Create model

     oController.createModel();

     var oDataModel = sap.ui.getCore().getModel('myModel');


     oDataModel.attachRequestSent(function(){

          console.log("Sent!");

     });

     oDataModel.attachRequestFailed(function(){

          console.log("Failed!");

     });

     oDataModel.attachRequestCompleted(function(){

          var requestTypeID = oDataModel.getProperty("/InputData('"

               + taskId + "')/HandleRequestType/Request/RequestID");

     

          this.buildForm(oController, requestTypeID);

     });

}

I'm really lost.

The strange thing is that the model retrieves the values from the oData service (if I directly put the buildForm() in my createContent() method)!

Qualiture
Active Contributor
0 Kudos

Hi Thorsten,

I'm not entirely sure, but I think you may try to add these event handlers during the model instantiation (in the controller), not after it already has been set as the core model

Not sure how your createModel() function looks like, but I would attach the event handlers there:


createModel: function() {

    var oModel = new sap.ui.model.odata.ODataModel(yourUrl);

    oModel.attachRequestSent(function() {

console.log("Request sent");

        //etc

    });

    oModel.attachRequestCompleted(function() {

  console.log("Request completed");

        //etc

    });

    oModel.attachRequestFailed(function() {

  console.log("Request failed");

        //etc

    });

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

}

Former Member
0 Kudos

Hi Robin,

I've added my event handlers to the createModel() method:


createModel : function() {

     var taskId = getValueOfURLParameter("taskId");

     // Create ODataModel for BPM Task Data OData service

     var taskDataSvcURL = "/bpmodata/taskdata.svc/" + taskId;

     var oDataModel = new sap.ui.model.odata.ODataModel(

          taskDataSvcURL, true);

     oDataModel

          .setDefaultBindingMode(sap.ui.model.BindingMode.TwoWay);

     oDataModel.attachRequestSent(function(){

          alert("Sent!");

     });

     oDataModel.attachRequestFailed(function(){

          alert("Failed!");

     });

     oDataModel.attachRequestCompleted(function(){

          alert("Completed!");

          var requestTypeID = oDataModel.getProperty("/InputData('"

               + taskId + "')/HandleRequestType/Request/RequestTypeID");

// this.buildForm();

     });

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

}

Now the RequestSent and the RequestComplete handlers get called!

Also the variable requestTypeID is filled correctly.

The only remaining issue:

How can I now build my form from the attachRequestCompleted() event?

Is it a good approach to build my form in the Controller.js file (via a buildForm() method)?

Or how can I call a method in my View.js from the Controller.js?

One weird thing at the end :

If I set the model with a name


sap.ui.getCore().setModel(oDataModel, "myModel");

the oData service is not even called!!

And I even don't have any sap.ui.getCore().getModel("myModel") in my whole coding!

If I change it back to


sap.ui.getCore().setModel(oDataModel, "myModel")

everything works as expected!

What's the reason for that?

Qualiture
Active Contributor
0 Kudos

Hi Thorsten,

What I normally do (since I'm using XMLViews I cannot even use methods in my view) is to build all the view parts in the view itself, and bind the visible property of the surrounding containers to a JSONModel dedicated to UI operations (visible, enabled, readonly, etc)

For instance,


var uiData = {

    lytVisible : false,

    btnEnabled : true,

    //etc

}

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

oUIModel.setData(uiData);

sap.ui.getCore.setModel(oUIModel, "UIModel");

Then in the view I define all the needed extra controls:


var oLayout = new sap.ui.commons.layout.MatrixLayout({

    visible : "{UIModel>/lytVisible}"

});

and in your controller you then set (for instance, in the attachedRequestCompleted handler) :


sap.ui.getCore.getModel("UIModel").setProperty("/lytVisible", true);

I'm not sure why the OData named model wouldn't work though...

Former Member
0 Kudos

Thanks again!

I really have the problem that my oData request is not finished when createContent() of my view is executed.

But I need some data from the oData model to know how the form should look like (e.g. show only details of an Exception request if Request type == "Exception request"...).

Maybe I've to set some controls invisible after the oData request is completed...

Qualiture
Active Contributor
0 Kudos

That was basically what I was trying to explain (but with everything invisible at first, and only show what you need after the request has completed)

Nigel_James
Active Contributor
0 Kudos

Nothing like replying to a thread over a 18 months old but I generally attach these methods in my onInit() thus:


this.oModel.attachRequestFailed(jQuery.proxy(this.doRequestFailed, this));

Where doRequestFailed is defined in the class later on.

Just a note for someone else stumbling across this thread in the next 18 months or so

Answers (1)

Answers (1)

Former Member
0 Kudos

This message was moderated.