cancel
Showing results for 
Search instead for 
Did you mean: 

Calling methods of using parent controller

Former Member
0 Kudos

Hi,

I have a master component A and have declared couple of methods as interface methods. in this component i dynamically do component usage B. i want to know if there is a way that component B can call the interface methods of its parent component A at runtime? I did not find any documentation on how to get the reference of current component (parent component A) interface and send it to used component.

any guidance is appreciated.

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

Hi Chris,

Thanks for your response, this looks like the only way.

ChrisPaine
Active Contributor
0 Kudos

Hi Prashant,

Component A is not a used component of component B - so component B will not be able to address it. You could find ways around this by passing a reference of component A in your call - but it probably wouldn't be good practice.

Instead, think about raising an event in component B and listening for this in component A. The event listener could update the context which component B could then reference. This way component B remains agnostic about which component is calling it. (could be component C for example and you do not get the case where component B tries to call a non-existent method in the calling component.).

This is the way that other used components communicate with their parents - think of freely programmed value helps as an example.

Hope this helps.

Cheers,

Chris

Former Member
0 Kudos

Hi Chris,

can you please send sample code on how to pass reference of master controller interface to the component B (used component).

ChrisPaine
Active Contributor
0 Kudos

Hi,

I previously said that it was probably bad practice to do that - so I'm not going to encourage bad coding practice by explaining how to implement it!

However, I have had a look at the link that Abhi posted - it is an excellent description on how to use events, and I would strongly recommend that instead you use that process.

If you must call parent methods from the child - then use an approach like the FPM does - create a class that has a static reference to the main instance - which you can then reference and use to execute logic in the main component. This is the way that error messages are propagated through to the parent component in the FPM for example.

That said - events are your friend here - and when you have such excellent reference material it would be foolish to go past them.

Cheers,

Chris

Former Member
0 Kudos

Hi Chris,

if you see, my requirement is similar to FPM framework. so how do you think that it will be bad a practice?

in FPM you are able to call the parent component using IDR methods to set the title of the page. no event handler is required.

All i am asking is same thing, how do you get the reference of parent component into the child component.

as far as event handling, as per documentation, i have to declare component usage of parent component to handle events of that component. in my case i will not know who the parent component is. Abhi's link only shows event handling within component.

if you can point me to the code in FPM where it passes the static reference of master component instance or any way to get that master component instance from child component, that will be helpful.

ChrisPaine
Active Contributor
0 Kudos

>in FPM you are able to call the parent component using IDR methods

That's not entirely the case - you call the FPM class - store values in that class which are then read by the FPM controller.

But building a framework like the FPM for something which can be easily achieved using eventing seems crazy.

The concept of events is the same whether you use the same component and views eventing, or multiple components. Perhaps Ahbi might be tempted to update his article to show cross component eventing

(post split)

ChrisPaine
Active Contributor
0 Kudos

>as far as event handling, as per documentation, i have to declare component usage of parent component to handle events of that component

not really - as you raise the event in the child and handle it in the parent - so the parent must know which child event to listen for, but the child has no need to know which component is listening for the event. The parent can then update the context of the child in response to the event if any feedback to the child is required - or just update its own context if that is all that is required.

As previously mentioned this is pretty much how the freely programmed value helps work - there are plenty example of cross component eventing in this particular area that you should be able to find.

Hope this helps,

Chris

Former Member
0 Kudos

Hi Chris,

thanks for the response.

Abhi: can you mention event handling for below scenario:

Component A: parent component with view/context to display page title. This component should be able to listen event 'CHANGE_TITLE' and accordingly set its own context.

Component B: initial screen of application. This component will be directly used by component A (Component A will dynamically embed component B). Component B should be able to raise 'CHANGE_TITLE' event.

Component C: this component is used within component B. component C should be able to raise 'CHANGE_TITLE' event.

in short component A should be able to listen all CHANGE_TITLE event during runtime. however usage for only component B will be declared under it.

ChrisPaine
Active Contributor
0 Kudos

If component A doesn't know about component C, the easiest way would be for component A to listen for the event from component B, and for component B to listen for the event from component C. If component B finds that the event is triggered in component C, then it should raise its own "CHANGE_TITLE" event, which component A will then pick up and update the related context element in component A.