You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@flex.apache.org by Tintin <19...@googlemail.com> on 2014/10/14 22:19:26 UTC

Model View Presenter Event Handling

Hello

How should a parent component handle an event raised by a child component?

I have a custom mxml component (let's call it parent_view) containing 2
further child custom components (child1_view and child2_view). Each of these
components have their own ActionScript Presenter class.

child1_view has a button and I want to change the currentState of
parent_view when it is clicked.

In my mind the 'views' know of their respective 'presenters' who's job it is
to pass data back to the Model and handle events raised by the Model to
change their property values to which the 'views' are bound to (causing the
'views' to update. The 'presenters' know nothing of their respective
'views', the Model knows nothing about the 'presenters' or the 'views'.

I imagined that my child1_view button.click event would call a function in
child1_presenter. child1_presenter function would dispatch a custom event
such as child1ButtonClicked. parent_presenter would have an event handler
listening for the child1ButtonClicked event and change the currentState of
the parent_view. What I can't figure out is how I get a reference to the
child1_presenter class within my parent_presenter in order to add the
eventHandler?

Currently the way I have got it working is as follows: Following child1_view
button.click the function in child1_presenter gets a reference to the
currentTarget of the event argument passed. From this reference I can obtain
the parent and simply set the currentState, no events necessary. I don't
think this is very good practice though!

I'm sure the answer is simple, but I can't see it.

Thanks

Chris



--
View this message in context: http://apache-flex-users.2333346.n4.nabble.com/Model-View-Presenter-Event-Handling-tp8416.html
Sent from the Apache Flex Users mailing list archive at Nabble.com.

Re: Model View Presenter Event Handling

Posted by Tintin <19...@googlemail.com>.
Further reading and testing has possibly brought me to a better solution, I
welcome your thoughts though.

In child1_view I have declared a custom event as follows:



The button in child1_view has its click handler defined as follows:



In parent_presenter I have a function which is called on creation complete
of the associated parent_view as follows:



Sure enough the event raised when I click the button in the child1_view is
handled by the 'onReturnLabelClicked' function as defined in the previous
addEventListener method.



--
View this message in context: http://apache-flex-users.2333346.n4.nabble.com/Model-View-Presenter-Event-Handling-tp8416p8419.html
Sent from the Apache Flex Users mailing list archive at Nabble.com.