SAP / openui5-docs

OpenUI5 Markdown Documentation
https://sap.github.io/openui5-docs/
Creative Commons Attribution 4.0 International
73 stars 58 forks source link

Using `sap.ui.core.Fragment.load` with the `controller`-property #97

Open pubmikeb opened 8 hours ago

pubmikeb commented 8 hours ago

While doing a codebase refactoring to get rid of the legacy UI5 APIs, I've faced a need to replace sap.ui.xmlfragment with the modern, async API — sap.ui.core.Fragment.load.

In the sap.ui.core.Fragment-documentation, I've found several snippets how to use this API, e.g.:

sap.ui.require(["sap/ui/core/Fragment"], async function (Fragment) {
    const myFrag = await Fragment.load({
        name: "my.useful.VerySimpleUiPart",
    });
});

In my case, this snippet didn't work until I've added controller: this, following the @boghyon's advice on SO.

In the API documentation, there is a mention of the controller property:

the Controller or Object which should be used by the controls in the Fragment. Note that some Fragments may not need a Controller while others may need one and certain methods to be implemented by it.

Since in some circumstances the code won't work without the controller-property, wouldn't be better to pay more attention to this property in this API's documentation, providing a code snippet and more detailed explanation when and why this property might be necessary, rather than just mentioning «some Fragments may not need a Controller while others may need one»?

boghyon commented 7 hours ago

didn't work until I've added controller: this

Better replace sap.ui.xmlfragment with this.loadFragment, as shared in "Methods assigned in XML fragment not triggered" - Stack Overflow, if this can be referred to a Controller instance which becomes the event listener of the fragment automatically.


API reference: sap/ui/core/mvc/Controller#loadFragment

pubmikeb commented 7 hours ago

Better replace sap.ui.xmlfragment with this.loadFragment

Then, there is no need for an extra call of sap.ui.require with loading of sap/ui/core/Fragment!

Then perhaps it's better to mention in the documentation that this.loadFragment is the preferable alternative over sap.ui.core.Fragment.load to replace sap.ui.xmlfragment.

Currently, there is no single word regarding this.loadFragment in the sap.ui.xmlfragment-section.