cancel
Showing results for 
Search instead for 
Did you mean: 

SAPUI5: How to call a function from within a Controller

Former Member
0 Kudos

Hi guys,

we've been developing an SAP UI5 application which adheres to the MVC paradigm.

In our JS view we placed a button:

var createOrderBtn = new sap.ui.commons.Button({

                                                                                text : "Create",

                                                                                press : oController.onCreate

});

Now in the controller:

$.ajax({

               type : "POST",

                url : url,

               data : formData,

               contentType : "application/json; charset=utf-8",

               dataType : "json",

               success : createCallback

});

createCallback: function() {

                    alert('createCallback returned successfully');

          },

This raises an exception at runtime since createCallback is not known from within the button (which is placed into the view) but it's created within the view's controller.

How can we work this out?

Thank you.

Regards

View Entire Topic
Former Member
0 Kudos

hi,

I think here is the solution which can help you.

In View:

var createOrderBtn = new sap.ui.commons.Button({  text : "Create", press : oController.onCreate });

In controller:

use this inside ajax :-  success : this.createCallback


if  "this.createCallback " will not work ,then you can use :-

sap.ui.controller("viewname-process.viewname").createCallback

sap.ui.controller("viewname-process.viewname") - This line you can find as 1st line in your controller.

Thanks

Gouri Shankar

Former Member
0 Kudos

It worked..Gouri..

jianwang
Explorer
0 Kudos

sap.ui.controller("viewname-process.viewname") is still a little different from the actual controller. We cannot use sap.ui.controller("viewname-process.viewname").byId("comp_name") to get the specified controller.

In another word, sap.ui.controller("viewname-process.viewname") doesn't have the binding oView. Is it a known issue? or Do you know the reason? Thanks!

former_member197070
Discoverer
0 Kudos

Hi Jian,

Topic is from a while ago, but I thought it might still be useful for others looking at it and facing the same issue.

When calling sap.ui.controller("controllername") a new controller object is created and thus, loosing the binding with the model / view. In order to refer to the existing controller you should pass a variable refering to the actual controller object.

E.g. when using a confirmation dialog


sap.ui.define([

   "sap/ui/core/mvc/Controller",

   "sap/ui/model/json/JSONModel",

   "sap/ui/commons/MessageBox"

 

], function (Controller,JSONModel) {

  "use strict";

  return Controller.extend("my.controller.App", {

       onUploadData : function(oEvent){

       //Pass actual object to 'that' variable

       var that = this;

       //Call confirmation dialog

       sap.ui.commons.MessageBox.show(

            "Requesting confirmation",

            sap.ui.commons.MessageBox.Icon.INFORMATION,

            "Confirmation pop-up",

            [sap.ui.commons.MessageBox.Action.YES, sap.ui.commons.MessageBox.Action.NO],

            function(response) {

                 //Now use 'that' variable to execute controller function

                that.execute(response);

            }

       );

  },

  execute : function(oEvent){

       //Do something else

  }

  });

});

jianwang
Explorer
0 Kudos

Thanks Thomas. Your reply is very useful.

0 Kudos

Hi Thomas, I have four dialogs... in second dialog, I can get controller by `that`, but in the third dialog, `this` point to `button` view, what should I do ? Should I try sap.ui.getCore().byId("firstview ").getController().myMethod() ?