tags:

views:

546

answers:

2

This is how it is. I am getting XML data, I need to wait until it is parsed and then dispatch the event once the parsing is done from my library.

Firstly is there any way to avoid events in library in this case?

An alternative I saw was to use VOs. So what is this and how to make it work.

A: 

If you're getting your result as XML data from flex, I'm guessing you're using and HTTPService that gets the xml, so you access the data in the result property of a ResultEvent.

e.g.

private function resultHandler(e:ResultEvent):void{}

You would get your data:

private function resultHandler(e:ResultEvent):void{
   var tempCollection:ArrayCollection = new ArrayCollection();
   tempCollection = e.result.someDataObject as ArrayCollection;
}

and this would the place you would set the data you get from xml into VO's

private function resultHandler(e:ResultEvent):void{
  var tempCollection:ArrayCollection = new ArrayCollection();
  tempCollection = e.result.someDataNode as ArrayCollection;
for each(var item:Object in tempCollection){
  var myVO:VO = new Image();
  myVO.firstProperty = item.firstProperty;
  myVO.secondProperty = item.secondProperty;
  myVOCollection.addItem(myVO);
 }
}

The idea is simple...a VO is just a custom Object: a class you create extends Object and has the purpose to store values from an external data source( e.g. you xml result ). Since you're using a custom class that is faster than using a dynamic class and it helps a lot when reading the code and debugging( you get data type checking and all that).

They can be anything: products in a store, photos in a gallery, etc.

In the example I assumed someDataNode is a node in you xml and, myVOCollection, an ArrayCollection for your data and so on.

so a VO in this case you be something like:

package{
class VO{

private var _firstProperty:String;
private var _secondProperty:String;

public function VO(firstProp:String=null,secondProp:String=null){
_firstProperty = firstProp;
_secondProperty = secondProperty;
}

public function get firstProperty():String{
return _firstProperty;
}

public function set firstProperty(value:String):void{
_firstProperty = value;
}

public function get secondProperty():String{
return _secondProperty;
}

public function set secondProperty(value:String):void{
_secondProperty = value;
}

}
}

Your Model class would probably manage loading and parsing of data, and once that is done it will dispatch an event to let the application know the data requested is available.

In as few words as possible a valueobject in flex would be an actionscipt class that represents an item of data. Using one means mapping a generic object ( that comes in from an external source) to its actionscript representation.

Nothing fancy.

Hope it helps.

George Profenza
VOs are in no way tied to an external data source. They are extremely useful for locally generated data also.
Joel Hooks
+1  A: 

If you can use AMFPHP I would highly recommend it. You can define Value Objects (VO) on both the server and in actionscript. This will allow you to pass strongly typed objects back and forth from client to server. There is no need to parse, use e4x, or suffer in that way at all.

VOs are also referred to as DTO (data transfer objects), booth of which are object oriented design patterns.

Joel Hooks