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