0
votes

I'm working on creating a simple MCV application, in order to understand MVC better. The problem I'm having is registering event listeners. The way I see MVC is that the view dispatches events, the controller listens for these events and acts on them, either updating the model or amending the view. So in my MVC app I have a controller which during initialisation should register all the events that are dispatched by the view. This is my controller:

public class AppController extends UIComponent { private var _model:StaffAppModel;

    public function AppController( model:StaffAppModel)
    {
        trace( "controller created" );
        this._model = model;

        // start up register event listeners
        this.addEventListener( "saveUserEvent", saveUserHandler );      
    }

    // event handler
    public function saveUserHandler( e:SaveUserEvent ):void
    {
        trace("saveUserHandler run");
        trace( e._userObj.getFirstname() );     
    }
}

The problem is in my client file (I'm writing a Flex based AIR app, so I have a MXML file). In this MXML file I initialise the controller and add the view components.

        private function onInit():void
        {
            var _model:StaffAppModel = new StaffAppModel();
            var _controller:AppController = new AppController( _model );

        }
    ]]>
</mx:Script>

<mx:VRule horizontalCenter="-56" bottom="10" top="10" width="1" strokeColor="#000000"/>

<comp:DetailsView x="10" y="17" width="325" />
<comp:StaffListView x="352" y="10" height="570" width="400"/>

Then I dispatch events from these view components. Here's an example of a view dispatching a event:

public function saveUser():void { _userObj.setFirstname( firstnameTxt.text ); _userObj.setLastname( lastnameTxt.text ); _userObj.setJobtitle( jobTitleTxt.text ); var evt:SaveUserEvent = new SaveUserEvent( _userObj );

            dispatchEvent( evt );
        }

Now is this the way to do this, and why is it that my controller is not registering the event handlers?

Thanks

2

2 Answers

1
votes

Since your controller isn't on the view stack, it never has a chance to catch the event. Typically you want to have a reference to your view in your controller so you can attach event listeners directly. Another approach would be to create a singleton dispatcher class that extends EventDispatcher . That way you could dispatch and listen for events in a central hub and everything could remain de-coupled. For example your view could go:

myDispatcher.dispatchEvent(new MyEvent( MyEvent.SOMETHING_AWESOME) )

and your controller:

myDispatcher.addEventListener(MyEvent.SOMETHING_AWESOME, awesomeHandler);
0
votes

I think you got registering of your events a bit mixed up. If you say:

this.addEventListener( "saveUserEvent", saveUserHandler );

That means that the event will trigger if "this" dispatches the event. I think you want to add the event listener to your views, like this:

myView.addEventListener( "saveUserEvent", saveUserHandler );

Then when "myView" dispatches the "saveUserEvent" the saveUserHandler function will be invoked.

Hope this helps you.