Wednesday, September 7, 2011

Robotlegs - Accessing a view's objects from another view

 We can not directly access a view's object from another view mediator.

To achieve this, we need to dispatch the events & then access the view's objects from another view.

You can not inject a view object in another view mediator & access.

Sample:

I have a MainView which has viewstack consisting of 3 childs (1.UserScreen 2.ProjectsScreen 3.ConfigScreen)

Now i am in ProjectsScreen & on click of a button, want to display the UserScreen by changing the viewstack index on MainView.

In ProjectsScreenMediator you can inject MainScreen view & then change the ViewStack selectedIndex.

You have to trigger an event so that, MainScreenMediator listens & changes the ViewStack selectedIndex through MainScreenMediator.

Simply, ProjectsScreenMediator accesses the MainScreenMediator by dispatching an event from ProjectsScreenMediator.

ProjectsScreenMediator code:

public class ProjectFormMediator extends Mediator
{
   [Inject]
   public var projFormView:ProjectForm;

   override public function onRegister():void
   {
      eventMap.mapListener(projFormView.projFormCancel, MouseEvent.CLICK,   onCancelClicked);
   }

   private function onCancelClicked(event:MouseEvent):void
   {
      eventDispatcher.dispatchEvent(new ProjectFormEvent(ProjectFormEvent.CANCEL_PROJECT));
   }
}
MainScreenMediator code:
public class MainViewMediator extends Mediator
{
   [Inject]
   public var view:MainView;
   override public function onRegister():void
   {
      eventMap.mapListener(eventDispatcher,ProjectsEvent.NEWPROJECT_CLICKED,  onNewProjClicked);
   }
   private function onNewProjClicked(event:ProjectsEvent):void
   {
       view.mainVS.selectedIndex = 0;
   }
}

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...