English 中文(简体)
registering event listeners in a controller
原标题:

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

最佳回答

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);
问题回答

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.





相关问题
Disable button tooltip in AS3

I want to disable the tooltip on certain buttons. The tooltip manager seems to be an all or nothing solution. Is it possible to disable the tooltip for just one or two buttons?

Multiple Remote call made simultenously

I was making multiple remote calls and they are done sequentially and when I am getting a result event back it s triggering calls to all the methods with ResultEvent as an argument . I am supposed to ...

Attaching a property to an event in Flex/AS3

I have a parameter that needs to be passed along with an event. After unsuccessful attempts to place it on the type by extending the class, I ve been advised in another SO question to write a custom ...

Clearing RSL in Cache

I have built a flex application which has a "main" project and it is assosciated with a few RSL s which are loaded and cached once i run my "main" application. The problem i am facing is that the ...

What s a good way of deserializing data into mock objects?

I m writing a mock backend service for my flex application. Because I will likely need to add/edit/modify the mock data over time, I d prefer not to generate the data in code like this: var mockData =...

AS3 try/catch out of memory

I m loading a few huge images on my flex/as3 app, but I can t manage to catch the error when the flash player runs out of memory. Here is the what I was thinking might work (I use ???? because i dont ...

热门标签