model is not being listened to
This is my first RL application. I kept this as simple as possible and I have gone though the documentation, but I can't see why this isn't working.
The application is trying to get data from a command and display it as soon as the startup is complete.
// MAIN
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:local="*"
minWidth="955" minHeight="600"
>
<fx:Declarations>
<local:ThinSliceContext contextView="{this}" />
</fx:Declarations>
<local:ThinSliceView/>
</s:Application>
// CONTEXT
package {
import flash.display.DisplayObjectContainer;
import flash.events.DataEvent;
import org.robotlegs.base.ContextEvent;
import org.robotlegs.mvcs.Context;
public class ThinSliceContext extends Context {
public function ThinSliceContext() {
super();
}
override public function startup():void {
commandMap.mapEvent(ContextEvent.STARTUP_COMPLETE
, GetCommand, ContextEvent);
mediatorMap.mapView(ThinSliceView, ThinSliceMediator);
injector.mapSingleton(ThinSliceModel);
super.startup();
}
}
}
// VIEW
<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx">
<s:FormItem label="Thin Slice:">
<s:Label id="lblThinSlice"/>
</s:FormItem>
</s:Group>
// COMMAND
package {
import org.robotlegs.mvcs.Command;
public class GetCommand extends Command {
[Inject]
public var model:ThinSliceModel;
override public function execute():void {
model.data = "started";
}
}
}
// MODEL
package {
import org.robotlegs.mvcs.Actor;
public class ThinSliceModel extends Actor {
public var _data:String = "Initially empty";
public function set data(d:String):void {
_data = d;
dispatch(new ThinSliceModelEvent(ThinSliceModelEvent.MODEL_CHANGED));
}
public function get data():String {
return _data;
}
}
}
// EVENT
package {
import flash.events.Event;
public class ThinSliceModelEvent extends Event {
public static const MODEL_CHANGED:String = 'modelChanged';
public function ThinSliceModelEvent(type:String, bubbles:Boolean=false
, cancelable:Boolean=false) {
super(type, bubbles, cancelable);
}
override public function clone():Event {
return new ThinSliceModelEvent(type, bubbles, cancelable);
}
}
}
// MEDIATOR
package {
import org.robotlegs.mvcs.Mediator;
public class ThinSliceMediator extends Mediator {
[Inject]
public var view:ThinSliceView;
[Inject]
public var model:ThinSliceModel;
public function ThinSliceMediator() {
super();
}
override public function onRegister():void {
eventMap.mapListener(eventDispatcher
, ThinSliceModelEvent.MODEL_CHANGED
, handleData, ThinSliceModelEvent);
}
private function handleData(e:ThinSliceModelEvent):void {
view.lblThinSlice.text = model.data;
}
}
}
What did I forget here?
Comments are currently closed for this discussion. You can start a new one.
Support Staff 2 Posted by Ondina D.F. on 16 May, 2012 07:10 AM
Hey Chris :)
There is a very long discussion on this topic:
http://knowledge.robotlegs.org/discussions/problems/353-mediator-ca...
The short answer:
The Command, GetCommand, runs before ThinSliceMediator.onRegister(), thus the Mediator can’t hear the event dispatched by the Model.
To make sure the Mediator is ready, you can:
-dispatch a custom event after FlexEvent.APPLICATION_COMPLETE has fired
-or within the ThinSliceMediator.onRegister() after mapping the event
In your Context.startup():
contextView.addEventListener(FlexEvent.APPLICATION_COMPLETE, onApplicationComplete);
instead of
commandMap.mapEvent(ContextEvent.STARTUP_COMPLETE, GetCommand, ContextEvent);
Then in the handler:
protected function onApplicationComplete(event:Event):void
{
dispatchEvent(new ThinSliceModelEvent(ThinSliceModelEvent.GET_MODELS_DATA));
}
One more thing. I know it’s just a test application, but even so it would be better to use the recommended mvcs packages, to avoid possible naming conflicts.
Ondina
Chris closed this discussion on 10 Jul, 2012 03:40 AM.