views:

29

answers:

1

How do you bind an event listener from the controller to the view's elements eg button (click event) to its own handler?

Originally I was doing this from the view eg.

button.addEventListener(MouseEvent.CLICK, controller.buttonClick);

But now realise this is wrong since reading "each view is only supposed to "know" about the model which it represents, and "know" nothing of the controller"

A: 

model should have instance of this button, so controller will access model to add event listener, but view will only show this button, only add to stage.

Eugene
surely not as the model shouldn't have to know what gui the view has?
davivid
if we are talking only about data, then yes, but we have implemented UI components you should have some place to describe them, e.g. look at flex 4 spark skins, very nice architecture, where as models extended by mxml views, and you can add events inside of model or view, but if you want real controller logic, you should add *default* handlers to your UI components, which should be determined inside of model or view and then override them in controller with custom logic, or add inside of View if they are not the part of model and its a custom view component.once again check back spark skins.
Eugene
great thanks will look into it. What about if the only events I am interested in are KeyDown and MouseMove, Is it OK to simply add to the View: addEventListener(KeyboardEvent.KEY_DOWN, controller.keyDownHandler)
davivid
yes, so in such way you'll just define the calling way, but not the logic of controller, you are right. :) and don't forget to accept answer and rate comments)
Eugene
cool, think i have a much better grasp now. thanks.
davivid
you're welcome)
Eugene