
<core:FragmentDefinition xmlns="sap.m" xmlns:l="sap.ui.layout" xmlns:core="sap.ui.core"
controllerName="com.mjzsoft.FragmentTest.controller.Part1"> <!-- This part is redundant -->
<VBox>
<items>
<Text text="This part is from XML fragment"/>
<Button text="Call Function of Fragment" press=".Part1.onClick"/>
<Button text="Call Function of Parent Directly" press="onClick"/>
</items>
</VBox>
</core:FragmentDefinition>
<mvc:View controllerName="com.mjzsoft.FragmentTest.controller.View1" xmlns:html="http://www.w3.org/1999/xhtml" xmlns:mvc="sap.ui.core.mvc"
displayBlock="true" xmlns="sap.m" xmlns:l="sap.ui.layout" xmlns:core="sap.ui.core">
<App id="idAppControl">
<pages>
<Page title="{i18n>title}">
<content>
<l:VerticalLayout>
<l:content>
<core:Fragment fragmentName="com.mjzsoft.FragmentTest.view.Part1" type="XML"
id="prefix1"/>
<!--By using id you can add a fragment multiple times as it is used as a prefix in elements id-->
</l:content>
</l:VerticalLayout>
</content>
</Page>
</pages>
</App>
</mvc:View>
// Dialog1 uses the functions inside the parent controller
var oDialog1 = sap.ui.xmlfragment("com.mjzsoft.FragmentTest.view.myDialogFragment",
this);
// Dialog2 uses the functions inside of its own passed controller "com.mjzsoft.FragmentTest.controller.myDialogFragment"
var oDialog2 = sap.ui.xmlfragment("com.mjzsoft.FragmentTest.view.myDialogFragment",
"com.mjzsoft.FragmentTest.controller.myDialogFragment");
controllerName="com.mjzsoft.FragmentTest.controller.Part1"
.sap.ui.define([
"sap/ui/core/mvc/Controller",
"com/mjzsoft/FragmentTest/controller/Part1.controller"
], function (Controller, Part1) {
"use strict";
return Controller.extend("com.mjzsoft.FragmentTest.controller.View1", {
Part1: new Part1(this), // this pointer is window here and not the Controller
onInit: function () {
},
onClick: function (oEvent) {
console.log("I am in View1 Controller.");
}
});
});
<Button text="Call Function of Fragment" press=".Part1.onClick"/>
.Part1.
from the event handler name then it will call the functions inside the parent controller.Part1
variable in parent controller but it seems we will have 2 Part1
variables. One is created at the time the view is created and fragment elements are bound to that. And one other which is accessible by this.Part1
in the parent controller while is different from the one that elements are bound to.onInit
function and then inject it in the desired container or element:onInit: function () {
//Create new fragment with own controller
var oFragmentController = new FragmentController(this);
var oFragment = sap.ui.xmlfragment("com.mjzsoft.FragmentTest.view.Part1", oFragmentController);
this.getView().byId("myContainer").addContent(oFragment);
console.log(oFragmentController);
}
this
pointer of the parent controller and set it as a class variable inside the controller. Please note for this we need to define a constructor inside the fragment controller.sap.ui.define([
"sap/ui/core/mvc/Controller"
], function (Controller) {
"use strict";
return Controller.extend("com.mjzsoft.FragmentTest.controller.Part1", {
parent: null,
onInit: function () {
console.log("onInit of fragment will not call.");
},
onAfterRendering: function () {
console.log("onAfterRendering of fragment will not call.");
},
onClick: function (oEvent) {
console.log("I am in fragment controller");
},
constructor: function(oArg){
console.log("The fragment controller has been made");
console.log("You can pass some Argument to be set!!!", oArg);
this.oParent = oArg;
return Controller.call(this);
}
});
});
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
8 | |
7 | |
7 | |
6 | |
5 | |
5 | |
5 | |
5 | |
5 | |
4 |