cancel
Showing results for 
Search instead for 
Did you mean: 

Blocks (sap.uxap), how to add the associated controller?

juancarlosorta
Participant
0 Kudos

Hi all, I need to attach a controller to a block. I need an html control element within the block, and I need to reference it from a controller (this.getView().byId("main").setContent(content) ) .


I created the block folder in the source control of the application. I added a BlockName.js which extends BlockBase. I added a BlockName.view.xml view too. But now, I need to attach a controller to this view.

The view shown well, but when I add this line in the BlockName.view.xml for add the associated controller:


     controllerName="sap.ui.demo.myFiori.block.GoalsBlock"


It gives an error in Chrome and the console displays the following error:

    

     Uncaught Error: Controller sap.ui.demo.myFiori.block.GoalsBlock couldn't be instantiated


block Folder:


BlockName.js


jQuery.sap.declare("sap.ui.demo.myFiori.block.BlockName");

jQuery.sap.require("sap.uxap.BlockBase");

sap.uxap.BlockBase.extend("sap.ui.demo.myFiori.block.BlockName", {

    metadata: {

        views: {

            Collapsed: {

                viewName: "sap.ui.demo.myFiori.block.BlockName",

                type: "XML"

            },

            Expanded: {

                viewName: "sap.ui.demo.myFiori.block.BlockName",

                type: "XML"

            }

        }

    }

});

BlockName.view.xml

<core:View

controllerName="sap.ui.demo.myFiori.block.BlockNameController"

xmlns:core="sap.ui.core"

xmlns="sap.m">

<Label text="Test"/>

<core:HTML id="main">

      </core:HTML>

</core:View>

BlockNameController.controller.js

............................



Thanks

Accepted Solutions (0)

Answers (2)

Answers (2)

0 Kudos

Hi Juan Carlos Orta,

you need to rename your block controller from GoalsBlock to GoalsBlockController, afterwards it should work.

Of course you need to adjust the controller name also in the controller and in the view.


Cheers,

Dominik

juancarlosorta
Participant
0 Kudos

I got what I wanted with the rendered and onAfterRendering methods in BlockName.js.

But anyway, I still like to know how to add the associated controller.

0 Kudos

Hi Juan Carlos Orta,

Hope this link will help you

http://help.sap.com/saphelp_nw75/helpdata/en/29/78f6064742456ebed31c5ccf4d051d/content.htm?frameset=...

Is it possible to associate fragment directly to Block instead of view?

Thanks and Regards,

Puneet Jain