cancel
Showing results for 
Search instead for 
Did you mean: 
Subscribe

Hello Expert,

I created a fragment and i can attach it to view using <core:Fragment fragementName="view.frg" type="XML" />

Now i can see the fragment in the view.

My requirement is to have controller for each created fragment.

I am following below link to get the details

JsDoc Report - SAP UI development Toolkit for HTML5 - API Reference - sap.ui

But i am not able to link the controller to the fragement. Please guide me to define the controller for each fragment.

Fragment:

<core:FragmentDefinition xmlns="sap.m" xmlns:l="sap.ui.layout" xmlns:core="sap.ui.core">

  <VBox>

  <items>

  <Text text="This dialog is from XML fragment" />

  <Button text="Clickme" press="onClick" />

  </items>

  </VBox>

</core:FragmentDefinition>

controller for fragment i have defined as:

sap.ui.define([

"sap/ui/controller"

], function(Controller) {

"use strict";

return Controller.extend("bind.controller.test", {

  onInit: function() {

  alert("test");

  },

  onClick: function(){

  alert("Thanks");

  }

});

});

Thanks,

Abhi

View Entire Topic
Former Member

Hello Everyone,

Any help is highly appreciated!

Thanks,

Abhi

francesco_alborghetti
Active Participant
0 Likes

Hi Abhi,

you can assign a specific controller to a fragment when you programmatically instantiate the fragment in your view controller


sap.ui.define([ 

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

  "Testing/controller/myFragmentController"

], function(Controller, FragmentController) { 

  "use strict"; 

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

     onInit: function() {

      //Create new fragment with own controller

      var oFragmentController = new FragmentController();

      var oFragment = sap.ui.xmlfragment("Testing.view.myFragment", oFragmentController);

      this.getView().byId("myPage").addContent(oFragment);

    }

  }); 

});

Here a plunker that shows how you can do it:

http://plnkr.co/edit/mZ60tE2WL3mTYhsff1av?p=preview

Former Member
0 Likes

Thank you so much Francesco.

This is working as expected.

Just a clarification: So all created fragments (with fragment controller) has to be included to the view programetticaly and not with static like <core:Fragment /> direclty to view ?

If we add the fragment direclty to xml view with <core:Fragment /> then we cannot add controller to it? ( I just did it and function was not getting called from fragment controller ).

Many Thanks for your input.

Abhi

francesco_alborghetti
Active Participant
0 Likes

Hi Abhi,

I don't think you can define your own controller when you instantiate a fragment in a declarative way into the XML, only the view controller can be referenced.

Former Member
0 Likes

Thankyou so much Francesco

ashish
Explorer

Although this is marked as answered, I'm not able to locate where the answer is. Can the author or someone help?