Futurescale, Inc. PureMVC Home

The PureMVC Framework Code at the Speed of Thought


Over 10 years of community discussion and knowledge are maintained here as a read-only archive.

New discussions should be taken up in issues on the appropriate projects at https://github.com/PureMVC

Pages: [1]
Print
Author Topic: Question on Code Reuse  (Read 7685 times)
Westside
Jr. Member
**
Posts: 12


View Profile Email
« on: December 18, 2008, 04:41:55 »

Hi,

I have a question on how I can correctly wire things up to get the most reuse out of some code I wrote.  My code works fine as it is but I am having trouble finding how how to reuse this code when say another combo box in a different view needs to show the same list of U.S. states.   So let's say I have "View A" and "View B" for purposes of this example.  "View A" and "View B" are two different views shown at two different times

I have the following code:

LoadStatesCommand.as

public class LoadStatesCommand extends SimpleCommand implements ICommand  {
      
    override public function execute(note:INotification):void      {
      var geographyProxy:GeographyProxy = facade.retrieveProxy( GeographyProxy.NAME ) as GeographyProxy;
      geographyProxy.loadUSStates();
   }
      
} // close class

GeographyProxy.as

public function loadUSStates():void {
   remoteDelegate.getUSStates();
}

public function result( event:Object ):void      {   
    switch ( event.token.message.operation )   {
       case "getUSStates":
         var ac_result:ArrayCollection = event.result as ArrayCollection;
         var obj:Object = new Object();
         obj.state_code = '';
         obj.state_name = 'Choose a State';
         ac_result.addItemAt(obj, 0);
         sendNotification( STATES_LOADED, ac_result );
       break;
  }
}

So the above works, and after the data comes back from my remote service I send a notification out (STATES_LOADED) and the mediator for "View A" has an interest in this notification and when it hears it, it handles it by taking the array collection and binding it to my comboBox.  I got this part working fine.

But now say the the user is looking at only "View B", it also has a comboBox that needs populating, how can I use the same code to populate it?  I can't send out the same notification otherwise "View A" will hear it which I don't want.

Hope I am making sense...

Thanks,

-Westside
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #1 on: December 19, 2008, 12:43:39 »

A lot of this has to do with timing and flow in your app if the states are loaded and some views are perhaps have their instantiation deferred.

If view a and b both exist, you could simply have their mediators be interested in the same loaded notification.

But it sounds like the states are already loaded and view a's mediator has already responded to that loaded by the time you navigate to view b, causing it to be created and have a mediator registered for it. That mediator's first job (in onRegister) should be to fetch the proxy, get the data and set it on its view component.

That is if you can always count on view a being navigated to first and b second.

Otherwise, the mediators for view a and b could both ask the proxy for the data by calling a method on the proxy, passing the result of this.getMediatorName() as one of the args.

The proxy, if it has the data already would send a note with the data setting the type property to the name of the mediator that was passed in. Both mediators are interested in the note, but only act if the type property of the note is equal to this.getMediatorName().

Also, for a population of state names, I'd use an Enum. Check out the EmployeeAdmin demo for an example of this. There are department and role enums there and they're used to populate the combo boxes.

-=Cliff>
Logged
Eric Arnold (umop)
Newbie
*
Posts: 4


View Profile Email
« Reply #2 on: December 20, 2008, 02:04:32 »

This is my first posting in this forum, so I may be way off here, but I wanted to throw an idea out there.  This could very well have been discussed elsewhere already, so my apologies if I'm being redundant on any of it.  I'm also very new to PureMVC, so I might be fundamentally wrong on something.  However, this seems like a reusable possible solution for data which may or may not be loaded at the time a form (or any visual object with children) has presented itself:

- State/Province view notifies that it's ready to populate fields to mediator
- State/Province mediator sends a notification to a command which:
    - Gets the proxy
    - Asks the proxy for cached results
    - If the cached results exist, just trigger sendCachedResults command through a notification (below)
    - If the cached results do not exist:
        - Proxy makes a request to the server to get the States/Provinces and sends a LOADING_DEPENDENCIES notification in case any of the mediators want to display a progress indicator.
        - Upon return of results from server, proxy caches results and triggers sendCachedResults command through a notification (below)
    - sendCachedResults command: sends notification of cached results to interested parties

Is it even possible that much of this could be generic so that the code could be reused multiple places and use the same commands and notifications, just changing the type on the notification for delivery.  If there is a concern about mediators / views which are not currently shown being given data out of turn (for performance reasons or memory constraints, etc), the mediators could check for their focus or visibility and ignore the notification if necessary.

Just an idea.  Your thoughts?

Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #3 on: December 20, 2008, 03:21:19 »

This all sounds about right. You've definitely got the gist of working with PureMVC.

-=Cliff>
Logged
Pages: [1]
Print