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: Best Practice for storing currently selected item?  (Read 4408 times)
codecraig
Full Member
***
Posts: 23


View Profile Email
« on: August 06, 2008, 09:15:32 »

I have a list and when I select an item I want to update a form.  So say the list has Person objects and when you select one a Form (in the same View) has a Name and Age field that get populated with the selected object.

Is it best to keep a, "private var selected:PersonVO" in the view or should I keep "selected" in the proxy?  If I keep it in the proxy, I'd have to have my view catch the "list change" event, fire off an event, the mediator would catch it and do "proxy.selected = view.list.selectedItem".  Then the proxy would maybe send out a notification, the mediator would catch the notification and populate the form.

Would the notification be defined in the ApplicationFacade?  (i.e. public static const PERSON_SELECTED:String ="personSelected");

Also, if it did go the route of sending an event and so on, I would need a "PersonSelectedCommand" right?  Since the mediator shouldn't talk directly to the proxy, it should use a notification.

Is one "better" than the other?
« Last Edit: August 06, 2008, 09:34:23 by codecraig » Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #1 on: August 06, 2008, 10:08:42 »

To see exactly this scenario in action, study the EmployeeAdmin demo in Flex or Python.

It has a datagrid of employees. Select one and the forms to edit details and modify roles become available.

-=Cliff>
Logged
codecraig
Full Member
***
Posts: 23


View Profile Email
« Reply #2 on: August 06, 2008, 11:40:39 »

exactly what I am doing now :)
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #3 on: August 06, 2008, 05:49:11 »

Sorry about that. Personally I tend to want the 'state' to be stored in the view components themselves if possible. Close to where that state is changed and acted upon. Otherwise the mechanisms for properly updating the Proxy(s) can become cumbersome.

Sometimes you have no choice but to explicitly put a 'selected' object somewhere that the whole app can get at it at different times for different purposes, but then its extremely important that it always truly reflect current selection and not be able to become stale. When you do a lot of this its just tends to get ugly.

If a strategy like the EmployeeAdmin demo is enough to alert other parts of the view that need it without having to resort to storing the selection that's already stored in the view, then avoid the mess by not trying to track the selection.

-=Cliff> 
Logged
philipSe
Sr. Member
****
Posts: 139


View Profile Email
« Reply #4 on: August 07, 2008, 02:48:21 »

Since the mediator shouldn't talk directly to the proxy, it should use a notification.

This idea is stated regularly in the forums.  It is not true.  Mediators CAN talk to proxies; they don't have to but they can.  See following taken from the Best Practices document.
Since Mediators will also frequently interact with Proxies, it is common for a Mediator to retrieve and maintain a local reference to frequently accessed Proxies in its constructor. This reduces repetitive retrieveProxy calls to obtain the same reference.
----Philip
Logged
Pages: [1]
Print