Skip to Content

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

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

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Oct 22, 2015 at 10:07 PM

    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.

    Add comment
    10|10000 characters needed characters exceeded

  • Apr 13, 2016 at 08:23 AM

    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

    Add comment
    10|10000 characters needed characters exceeded