How can i get exact view position from an another meditator class?

Ahmet Alsan's Avatar

Ahmet Alsan

23 Oct, 2012 05:50 PM

My startup command is:
var containerView:ContainerView = new ContainerView();
var sampleView:SampleView = new SampleView();

contextView.addChild(containerView);
contextView.addChild(sampleView);

containerView.y = 50;
containerView.x = 250;

SampleMediator:
[Inject] public var containerView:ContainerView;

trace(containerView.x); //0

Main:
public class Main extends Context
{

public function Main(contextView:DisplayObjectContainer)
{
    super(contextView);
}

override public function startup():void
{
    commandMap.mapEvent(ContextEvent.STARTUP_COMPLETE, StartupCommand, ContextEvent, true);

    injector.mapSingleton(SampleView);

    mediatorMap.mapView(ContainerView, ContainerMediator);
    mediatorMap.mapView(SampleView, SampleMediator);

    super.startup();
}

}

  1. Support Staff 1 Posted by Ondina D.F. on 24 Oct, 2012 07:30 AM

    Ondina D.F.'s Avatar

    Hi Ahmet,

    Views can communicate with each other through their Mediators via custom events.
    In the example below, SomeView dispatches SomeLayoutEvent and its SomeViewMediator redispatches the event on the shared event dispatcher. AnotherMediator listens for the event and accesses a method on its view, passing on the payload of the event.
    The payload can be anything you need it to be (Array, Point, VO).

    SomeLayoutEvent:

    public static const VIEW_POSITION_CHANGED:String="viewPositionChanged";
    
    public function SomeLayoutEvent (type:String, position:Point, bubbles:Boolean=false, cancelable:Boolean=false)
    {
        super(type, bubbles, cancelable);
        _position=position;
    }
    
    override public function clone():Event
    {
        return new SomeLayoutEvent (type, position, bubbles, cancelable);
    }
    public function get position():Point
    {
        return _position;
    }
    

    SomeView:

    var viewPosition:Point=new Point(this.x, this.y);
    dispatchEvent(new SomeLayoutEvent(SomeLayoutEvent.VIEW_POSITION_CHANGED, viewPosition));
    

    SomeViewMediator:

    override public function onRegister():void
    {
    eventMap.mapListener(view, SomeLayoutEvent.VIEW_POSITION_CHANGED, dispatch, SomeLayoutEvent);
    }
    

    AnotherViewMediator:

    override public function onRegister():void
    {
    eventMap.mapListener(eventDispatcher, SomeLayoutEvent.VIEW_POSITION_CHANGED, onViewPositionChanged, SomeLayoutEvent);
    }
    private function onViewPositionChanged(event:SomeLayoutEvent):void
    {
        view.changeViewPosition(event.position);            
    }
    

    AnotherView:

    public function changeViewPosition(position:Point):void
    {
        this.x=position.x;
        this.y=position.y;
    }
    

    Does this answer your question?
    Ondina

  2. 2 Posted by Ahmet Alsan on 24 Oct, 2012 08:18 AM

    Ahmet Alsan's Avatar

    Hello,
    Thank you very much!
    This solved my problem.

  3. Support Staff 3 Posted by Ondina D.F. on 24 Oct, 2012 08:35 AM

    Ondina D.F.'s Avatar

    You're welcome!

  4. Ondina D.F. closed this discussion on 24 Oct, 2012 08:35 AM.

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