Futurescale, Inc. PureMVC Home

The PureMVC Framework Code at the Speed of Thought


Welcome, Guest. Please login or register.
December 12, 2017, 08:28:28 PM
Home Help Search Login Register
News: Please DM @puremvc on Twitter to gain forum access. Spambots are why we can't have nice things.
Pages: [1]
Print
Author Topic: Best method for caching objects created by mediator  (Read 3509 times)
99miles
Jr. Member
**
Posts: 14


View Profile WWW
« on: September 15, 2009, 03:34:11 PM »

Hello-
I'm using AS3.
I have a Proxy that calls an HTTPService and returns with a bunch of information which the Mediator uses to draw lines. This can take some time, and since the user can hide/show the lines repeatedly by clicking a button, I want to be able to show the lines immediately instead of calling the service again. And as I have it now, I don't even have access to those lines anymore because they are created on the fly in the mediator with no references remaining accessible.

Sure, I could set a flag in my ApplicationFacade, as well as an array of lines I've created, then the next time the hide/show button is clicked I could check for that and if the array exists I can set the visibility of all the items in there to the appropriate state. But I don't feel like that a very good way. It's too tight of a dependency. How would you go about this?
Thanks!
Logged
99miles
Jr. Member
**
Posts: 14


View Profile WWW
« Reply #1 on: September 15, 2009, 04:55:41 PM »

It looks like after some more searching I found my answer...
http://forums.puremvc.org/index.php?topic=724.0

I'll give it a shot and report back if I still have questions.
Logged
Tekool
Sr. Member
****
Posts: 192


View Profile WWW
« Reply #2 on: September 16, 2009, 02:43:51 AM »

The best method for you here is probably to keep the drawing made by the lines in a Sprite that you add to the display list of the view that your mediator gives access. You can easily made it visible or invisible if needed. You also have to keep a local copy of the VO that gives you the data associated with the drawing.

When the proxy receives data it will send a notification. Your mediator will have access to a VO associated with the result. You can add a method to this VO that will compare itself to another of the same type. When received you use the VO.compare method to compare the new vo to the older. If they are the same, you just have to make the Sprite visible, if they are different you'll have to redraw it.
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW
« Reply #3 on: September 17, 2009, 09:40:39 AM »

You can cache that info locally in the view hierarchy as a VO, array or ArrayCollection. But if other disparate parts of the UI will need the info and don't have a reference to that view component that you stored the info in, you have a problem.

You can keep the info on hand in the Proxy that retrieved it, or if you'd like to keep the cached data separate from the remote service proxy, you could even register a bare proxy with the data.

In the handleNotification method of the Mediator that has received the data and thinks it may need to cache that data for awhile, you can do something like this:

Code:
case ServiceProxy.RESULT:
   var data:ArrayCollection = note.getBody() as ArrayCollection;
   myViewComponent.data = data;
   facade.register new Proxy("NowAndLaterData", data); // stash for later use
   break;

So when your component tells its Mediator that it needs data, the mediator just checks for the existence of the Proxy named "NowAndLaterData". If present it uses the cached data, otherwise it invokes the method on the ServiceProxy to fetch the data:

Code:
public function componentNeedsData(event:Event):void
{
   if ( facade.hasProxy("NowAndLaterData") ) {
      myViewComponent.data =  facade.retrieveProxy("NowAndLaterData").getData() as ArrayCollection;
   } else {
      serviceProxy.fetchData();
   }
}

Lots of folks complain that the Proxy class doesn't really give you anything since it's so simple; that you need to subclass to get any functionality at all, but that's not so. You can stash anything you want in the Model and retrieve it just this easily.

And note that the source of this data does not have to be a service. This could be data generated by the view for the view. One component might collect a bunch of settings for view layout and other parts of the view may wish to access those settings to adjust themselves accordingly. They are not part of the domain model, since they aren't persisted and are merely a way of the view organizing itself. A handy trick easy to pull off and is often overlooked.

-=Cliff>
« Last Edit: September 17, 2009, 09:47:43 AM by puremvc » Logged
Tekool
Sr. Member
****
Posts: 192


View Profile WWW
« Reply #4 on: September 17, 2009, 12:46:10 PM »

Quote
facade.register new Proxy("NowAndLaterData", data);

Brilliant ! Smiley
Logged
Pages: [1]
Print
Jump to: