Passing data between views

Vitor's Avatar

Vitor

21 Feb, 2013 12:40 AM

Hi,

I need to know if there are best practices to passing data between views, where one view is on stage, and another view will be created at runtime. Example:

I have one tab navigator which displays a list of objects. When user click in "view details" button, I'll add a new tab with the details data of that selected object. So, i need to create the view, after that, I need to add the object to the new view as follows:

var view: SomeView = new SomeView;

view.someProperty = "someValue";

var navigator:NavigatorContent = new NavigatorContent;

navigator.addElement(view);

someTabNavigator.addChild(navigator);

I need to do this, because the view doesn't at stage yet, so the mediator do not catch any event.

Is there another way to accomplish this? best practices?

Thank's!

  1. Support Staff 1 Posted by Ondina D.F. on 22 Feb, 2013 12:32 PM

    Ondina D.F.'s Avatar

    Hi Vitor,

    Sorry for the late response.

    I need to do this, because the view doesn't at stage yet, so the mediator do not catch any event.

    Here, 2 common solutions for robotlegs v1:

    A. The mediator of the view created dynamically dispatches an event requesting the data:

    1. UserListView->selectedItem as the payload of a custom event->UserListMediator->redispatches custom event->SomeCommand->passes the payload on to UserModel
      (or calls UserService to load user’s details and sets the results on UserModel)

    2. UserDetailsView gets added to the stage.

    3. UserDetailsMediator onRegister()
      ->dispatches custom event->AnotherCommand->UserModel->getUserDetails()->custom event-> UserDetailsMediator ->passes the payload on to UserDetailsView. UserDetailsMediator should register the listener for the event dispatched by the model before dispatching the event triggering the command in order to be able to listen to that event!!!

    B. Stray’s RelaxedEventMap (https://github.com/Stray/robotlegs-utilities-RelaxedEventMap) – probably the best solution

    Robotlegs v2 offers more possibilities. One of them is the view processor map extension, which allows injection of properties into views. [https://github.com/robotlegs/robotlegs-framework/tree/master/src/ro...]
    I’m used to Robotlegs 1' MVCS implementation where injecting into views wasn’t a very good idea, so I’m not sure yet about the best practices regarding this, but if you wanted to, you could do the following:
    Config:

    mediatorMap = injector.getInstance(IMediatorMap);
    viewProcessorMap = injector.getInstance(IViewProcessorMap);
    
    injector.map(SomeVO).asSingleton();
    
    viewProcessorMap.map(SomeView).toInjection();
    viewProcessorMap.map(ParentView).toInjection();
    
    mediatorMap.map(SomeView).toMediator(SomeMediator);
    mediatorMap.map(ParentView).toMediator(ParentMediator);
    

    ParentView

    [Inject]
    public var someVO:SomeVO;
    
    protected function parentList_changeHandler(event:IndexChangeEvent):void
    {
       someVO.someProperty = event.currentTarget.selectedItem.lastName;
       addNewViews();
    }
    protected function addNewViews():void
    {
       var someView:SomeView= new SomeView();
       addElement(someView);
    }
    

    SomeView

    [Inject]
    public var someVO:SomeVO;
    
    protected function creationCompleteHandler(event:FlexEvent):void
    {
       //do something with someVO.someProperty
    }
    

    Let me know if this helps or not.

    Ondina

  2. 2 Posted by Vitor on 23 Feb, 2013 04:01 PM

    Vitor's Avatar

    Hi Ondina, thank's for your reaply!

    I'm using Robotlegs V1, so the first approach sounds like more appropriate to me.

    But, if I have more than one UserDetailsView added to stage, when the user select an item in a list, all UserDetailsView will be updated, right? Is there a way to prevent this?

    I appreciate your help!

  3. Support Staff 3 Posted by Ondina D.F. on 24 Feb, 2013 06:34 PM

    Ondina D.F.'s Avatar

    Hi Vitor,

    No problem:)

    But, if I have more than one UserDetailsView added to stage, when the user select an item in a list, all UserDetailsView will be updated, right?

    That’s right.

    • If you added a listener for an event coming from a Service/Model in the onRegister() of the mediator, you can remove the listener in the handler method of that event: removeContextListener(SomeEvent.DATA_LOADED, onDataLoaded, SomeEvent); and this mediator will be deaf from now on:)

    • Or, in the hadler method you check against a Boolean

      if(view.needsData)
      {
      view.onDataLoaded(event.payload);
      view.needsData=false;
      }
      
    • Or, you decide which mediator should react to an event based on Views’ IDs.

    • or other solutions that don’t come to mind right now

    Ondina

  4. 4 Posted by Vitor on 24 Feb, 2013 09:13 PM

    Vitor's Avatar

    Ok, good job!

    But, I have one more question...

    How can I react to an event based on Views’ IDs?

    Best practices to do this?

  5. Support Staff 5 Posted by Ondina D.F. on 25 Feb, 2013 09:45 AM

    Ondina D.F.'s Avatar

    When you dispatch your event requesting data you pass the view’s UID along in the event’s payload.
    The event to which the mediator listens carries that UID back to the mediator.
    if (event.payload.viewId==view.id)
    {

    view.onDataLoaded(event.payload);
    }

    It depends on your use case and your preferences how you create and use those ids. You can assign an id for a view when you add it to the stage, directly or using a factory or you can keep a map (dictionary) of views’ ids in a model.
    Take a look at Joel’s example for inspiration:
    https://github.com/robotlegs/robotlegs-demos-Bundle/blob/master/Whe...

  6. 6 Posted by Vitor on 26 Feb, 2013 01:42 PM

    Vitor's Avatar

    Ok, this is it.

    Thanks Odina for your quick replies!

    I appreciate your help!

  7. Support Staff 7 Posted by Ondina D.F. on 26 Feb, 2013 03:14 PM

    Ondina D.F.'s Avatar

    My pleasure!

  8. Ondina D.F. closed this discussion on 26 Feb, 2013 03:14 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