Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass another controller when instantiating a fragment in SAPUI5

In the SAPUI5 / OpenUI5 xmlfragment documentation the third parameter is a controller for handling actions from the fragment.

This is critical for a dialog fragment where there are buttons to press etc.

Most of the time I have seen this instantiated as this or sap.ui.getCore().byId('<element>').getController())

See an example at Fragment not get correct Controller

Because of the complexity in a particular dialog I would like to have a separate controller for it.

I have looked around at this and had a few attempts but so far not successful.

I have put a working example on github of using this.

But I would like to instantiate Dialog.js as the controller for the Dialog.fragment.xml from initial.view.controller

Any takers?

Pull requests gladly received.

The Crux of the example is as follows (this is the initial.controller.js) :

sap.ui.controller("sc.test.view.initial", {

oDialog: null,

openTestDialog: function(){
    console.log("in open dialog");
     // instantiate the other controller
     var oDialogController = new sc.test.view.Dialog();
    // this next commented line is the 'normal' way to do it
    // oDialog = new sap.ui.xmlfragment( "sc.test.view.Dialog", this);  //oDialogController);
    // this is what I would like to achieve
    oDialog = new sap.ui.xmlfragment( "sc.test.view.Dialog", oDialogController);
    oDialog.open();
},       


onCartDialogCancel:function(oEvent){
// this function would then be in the other controller but how to get a handle on the dialog?
    oDialog.close();

}

});

Thanks.

like image 278
njames Avatar asked Nov 23 '14 22:11

njames


People also ask

Does fragment have its own controller?

A fragment, like an activity, has an XML layout file and a Java class that represents the Fragment controller.

What is the correct way to retrieve an element control by ID from a fragment?

Fragment is embedded into a view if it is part of the static view declaration. Controls inside this fragment are prefixed with view's ID. This is the same as controls which are directly placed in a view. In this most simple case, you can retrieve controls just as you retrieve controls inside a view.

How do you call a fragment in SAPUI5?

With the help of id, we can easily call the control of the fragment from the view controller file and in below code we try to achieve the same. In the controller. js file first define the Fragment and add the object to the function parameter. Next, add the function onOpenDialog.

What is controller in SAPUI5?

Controller extensions allow you to add functionality to existing applications. They can be used for extensibility purposes, for example by a customer wishing to extend SAP-delivered applications, or as a reusable part that is added to the original application.


1 Answers

(Just got to SYD airport)

All you're missing is the

jQuery.sap.require("sc.test.view.Dialog");

in your initial.controller.js.

Pushed a quick fix in a branch to your repo and opened a PR

like image 60
qmacro Avatar answered Oct 26 '22 19:10

qmacro