Robotlegs

249510573's Avatar

249510573

09 Aug, 2012 04:18 AM

Im my example, I have added a event listener into Context in onRegister() in GridDataMediator, but the mediator can not listening to the framework event. Why? please help me. The code is attached.

  1. 1 Posted by 249510573 on 09 Aug, 2012 04:33 AM

    249510573's Avatar

    The mediator onRegister() method runs after the view has landed on stage. But the view has been in the stage.

  2. Support Staff 2 Posted by Ondina D.F. on 15 Aug, 2012 11:08 AM

    Ondina D.F.'s Avatar

    Hello 249510573,

    I've had a look at your code.
    Possible solutions:

    • Stray’s RelaxedEventMap (https://github.com/Stray/robotlegs-utilities-RelaxedEventMap)

    • Ask for data within your Mediator’s onRegister() method: dispatch an event triggering a command, that calls the Service (after registering an event listener for the event dispatched by the Model in Mediator’s onRegister()):

    
    override public function onRegister():void
    {
    //first add the listener !!
    eventMap.mapListener(eventDispatcher, SystemEvent.LOADED_GRIDDATA, whenGridDataLoaded); 
    //then dispatch the event
    dispatch(new SystemEvent(SystemEvent.LOAD_GRIDDATA)); 
    }
    
    • make the service call on FlexEvent.APPLICATION_COMPLETE:

    In your FirstDemo.mxml add an event listener for FlexEvent.PREINITIALIZE:
    <s:Application …
    preinitialize="application_preinitializeHandler(event)" …

    Script:

    
    import demo.FirstDemoContext;
                
    import mx.events.FlexEvent;
    private var context:FirstDemoContext;
                
    protected function application_preinitializeHandler(event:FlexEvent):void
    {               
        context=new FirstDemoContext(this);
    }
    

    In your FirstDemoContext:

    
    public function FirstDemoContext(contextView:DisplayObjectContainer=null, autoStartup:Boolean=true)
    {   
        super(contextView, autoStartup);    
        this.contextView.addEventListener(FlexEvent.APPLICATION_COMPLETE, onApplicationComplete);
    }
    private function onApplicationComplete(event:FlexEvent):void
    {
    dispatchEvent(new SystemEvent(SystemEvent.LOAD_GRIDDATA));
    }
    

    Your DataGridMediator will be able to hear the event and to set dataGrid.dataProvider.

    If you want to get into details about the start up process of a Flex Application using the Robotlegs framework, you can take a look at the diagrams (especially pages 58-62) from this document:
    http://cloud.github.com/downloads/Ondina/robotlegs-incremental/robo...
    I have to say, that I never had the time to finish it or make it better, but, hopefully, it will at least give you an idea about the application’s availability and readiness to consume external services and respond to user input.

    • The disputed ‘Inject your Model into your Mediator’- if you choose this, then inject your Model against an interface. But, as I can see, you’re actually injecting DataGridModel into DataGridMediator and at the same time you’re letting DataGridModel dispatch SystemEvent.LOADED_GRIDDATA. You could avoid injecting the Model into the Meditor by sending dataGridModel.getData as the SystemEvent’s payload. By the way, SystemEvent is not a very good name for your custom event:)

    Your custom event would look like this:

    
    public class SystemEvent extends Event
    {
        public static const LOAD_GRIDDATA:String='loadGridData';
    
        public static const LOADED_GRIDDATA:String='loadedGridData';
    
        private var _someData:ArrayCollection;
    
        public function SystemEvent(type:String, someData:ArrayCollection=null, bubbles:Boolean=false, cancelable:Boolean=false)
        {
            _someData=someData;
            super(type, bubbles, cancelable);
        }
    
        public function get someData():ArrayCollection
        {
            return _someData;
        }
    
        override public function clone():Event
        {
            return new SystemEvent(type, someData, bubbles, cancelable);
        }
    
    }
    

    In your DataGridModel you’d do this:
    dispatch(new SystemEvent(SystemEvent.LOADED_GRIDDATA, getData));

    And in your DataGridMediator. whenGridDataLoaded():
    dataGrid.dataProvider = event.someData;

    If you want to know more about how the Robotlegs Mediators work, please read :

    https://github.com/robotlegs/robotlegs-framework/wiki/best-practice...

    http://knowledge.robotlegs.org/kb/reference-mvcs-implementation/are...

    and other articles under:
    http://knowledge.robotlegs.org/kb

    Is everything clear now? :)
    Ondina

  3. Ondina D.F. closed this discussion on 25 Aug, 2012 02:39 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac