openui5 icon indicating copy to clipboard operation
openui5 copied to clipboard

Enhancement request: Being able to assign listener module name to Fragment

Open boghyon opened this issue 7 years ago • 5 comments

Monitavtion

We can add fragments in declarative views, for example:

<core:Fragment id="..."
  fragmentName="my.view.fragment.Dialog"
  type="XML"
/>

This is fine but there is one drawback: It's not possible to assign an alternative event listener object from there [1] since the special property oController awaits an instance of type sap.ui.core.mvc.Controller, which can be done only in JS. This is often the main reason why application developers have to go back to a controller, call Fragment.load() , add the loaded control(s) to the view's <dependents> aggregation, and maintain other stuff.. all manually.

What is the expected result?

It would be nice if there is a property to which we can assign a name of the listener module, just like controllerName (View) or fragmentName. Something like this:

<core:Fragment id="..."
  fragmentName="my.view.fragment.Dialog"
  listenerName="my.controller.Dialog"
  type="XML"
/>

The resolved listener module should be then instantiated for that corresponding fragment, if it's a function / has a constructor. But it can be also a plain object with methods as discussed in https://github.com/SAP/openui5/issues/2267.


[1]: If the fragment is added in the view definition, the controller of the view is always assigned as the listener object automatically.

boghyon avatar Jan 08 '19 19:01 boghyon

One of the counterarguments against this proposal is often that the fragment wouldn't differ from a view with a controller.

From https://github.com/SAP/openui5/issues/2080#issuecomment-400278617:

Giving fragments a controller of their own makes them more complex (e.g. it raises the need for lifecycle hooks) and makes them more like views.

So the alternative approach is to use view instead of a fragment. Sometimes, though, (and also why I created this issue) a view cannot be applied to an aggregation where a specific control type is awaited.

For example: The type of the aggregation customHeader in sap.m.Page is sap.m.IBar.API Given a fragment with a sap.m.Bar as a root control, I could use that fragment as the customHeader content. If I have a view instead, it's not possible since the view doesn't implement sap.m.IBar.

boghyon avatar Jan 08 '19 19:01 boghyon

Hello @boghyon , Thank you for share your enhancement proposal. I've created an internal incident 1970022183. The status of the issue will be updated here in GitHub. Regards, Filip

fifoosid avatar Jan 09 '19 12:01 fifoosid

Hi @boghyon,

the argument is still that we want to keep that clear separation between a View and a Fragment. Fragment itself will not have any Controller except the one inherited from the View it is embedded in. I see the use case where you would like to fill an aggregation of certain type with a special behaviour also declaratively with a fragment.

Nevertheless, you should always be able to find the logic corresponding to the behaviour of your Controls in a View at the according Controller. Cross referencing to any other Controllers (or plain objects) is imo not contributing to a clear separation of concerns. So I see two possible options to overcome the issue in alternative ways:

  1. Use an XMLView containing the sap.m.Page where the Controller implements the handler
  2. Reuse the handler Code as kind of an extension for the Controller itself (via e.g. inheritance or mixin)

Referencing global objects is something I would not recommend and the option to reference a Controller by name is also, for the above mentioned reasons, no recommendation. Maybe one of the proposed alternatives is a way for you?

Best regards, Benedikt

bendkt avatar Jan 23 '19 15:01 bendkt

@bendkt Thank you for the response. I'll close this issue for now since the proposal won't be considered to be implemented.

boghyon avatar Jan 30 '19 16:01 boghyon

For other readers: As an alternative, the XMLCompositedoc could be used. But currently, it cannot be used in certain situations. See https://github.com/SAP/openui5/issues/2423.

XMLComposite is deprecated since 1.88.

boghyon avatar Mar 04 '19 08:03 boghyon