cancel
Showing results for 
Search instead for 
Did you mean: 

Receive the attribute component from the controller.

Former Member
0 Kudos

Hello!

Please tell me where I'm wrong.

I have created a component multiple angles and multiple controllers. I added an attribute 'main_data' to the component.

If I call this attribute in the code fragment, everything works correctly.

MonoTable_Edit.fragment.js


sap.ui.jsfragment("sap.ui.....forms.view.MonoTable_Edit", {

  createContent: function(oController) {

  var oDFC = oController.getOwnerComponent().main_data;

  ...

If I call this attribute in the code of the controller, the console browser (FireFox) I get the error:

"TypeError: this.getOwnerComponent is not a function"

MonoTable.controller.js


addNewEntry: function(){

  var o_entry;

  var component;

  component = this.getOwnerComponent();

  o_entry = component.main_data.output.lines[0];

  ...

Tell me, how else can you turn to the component from controller?

Accepted Solutions (1)

Accepted Solutions (1)

DK007
Active Participant
0 Kudos

Hi,

Try this:

var sComponentId = sap.ui.core.Component.getOwnerIdFor(this.getView());

var comp = sap.ui.component(sComponentId);

I used above statements to get component and called method on it.

Thanks,

Dheeram

Former Member
0 Kudos

Hi, Dheeram!

Now the console browser displays an error:

TypeError: this.getView is not a function

Perhaps should clarify the implementation of the controller:

MonoTable.controller.js


sap.ui.define([

  'jquery.sap.global',

  'sap/m/MessageToast',

  'sap/ui/core/Fragment',

  'sap/ui/core/mvc/Controller',

  'sap/ui/model/json/JSONModel'

], function(jQuery, MessageToast, Fragment, Controller, JSONModel) {

  "use strict";

  var MonoTableController = Controller.extend("sap.ui.x5.forms.controller.MonoTable", {

  onInit: function() {

  //...

  },

  onExit : function () {

  //...

  },

  //...

  testMethod: function(){

  var sComponentId = sap.ui.core.Component.getOwnerIdFor(this.getView());

  var comp = sap.ui.component(sComponentId);

  var s_tmp = comp.test_attribute;

  MessageToast.show(s_tmp);

  },

  });

  return MonoTableController;

});

former_member182372
Active Contributor
0 Kudos

how do you call testMethod

DK007
Active Participant
0 Kudos

Then looks like this is not reference of current controller. Where are you calling testMethod()??

Former Member
0 Kudos

Hello again, Maksim!

I call the method by pressing a button. Button is located in the fragment.

MonoTable_Edit.fragment.js


sap.ui.jsfragment("sap.ui.....forms.view.MonoTable_Edit", {

  createContent: function(oController) {

  //...

  oButtonNewEntry = new sap.m.Button({

      text: "Press me",

      press: oController.testMethod

     })

  oToolBar = new sap.m.Toolbar({

  content:[

     oButtonNewEntry    

  ]

  });

  oLayout = new sap.ui.layout.VerticalLayout("LayoutForMonoTableEdit", {

  content:[

  //...

     oToolBar

  ]

  });

  return oLayout;

  }

});

former_member182372
Active Contributor
0 Kudos

SAPUI5 SDK - Demo Kit

press: jQuery.proxy(oController.testMethod  , oController)



or


press: [oController.testMethod  , oController]

Former Member
0 Kudos

Thank you, Maksim! You helped me a lot!

Who will understand why it works that way. Thanks for the link!

former_member182372
Active Contributor
0 Kudos

well, that`s one of the reasons why use XML view, not JS 😉

Former Member
0 Kudos

Thank you, Dheeram, for trying to help!

The solution was found!

Answers (0)