Having multiple DataServices, each with their own getItem method (and createItem, deleteItem, fill and updateItem) means handling each event with an ItemReference. Since really the only things changing are the identity object, initial Object and the result handler, it is a perfect candidate for a wrapper class.
public class Services{
private var _personDS:DataService;
private var _workDS:DataService;
private var _instance:Services;
public static function getInstance(){
if (_instance == null) new Services();
return _instance;
}
public function Services(){
if (_instance == null) {
_instance = this;
_personDS = new DataService("my.person");
_workDS = new DataService("my.work");
}
else {
throw new Error("singleton class Services cannot accept multiple instances");
}
}
public function dsPerson():DataService{
return _personDS;
}
public function dsWork():DataService{
return _workDS;
}
public function getItem(ds:DataService,
identity:Object,
initObject:Object,
resFunction:Function) {
var token:ItemReference = ds.getItem(identity, initObject);
token.addResponder(new ItemResponder(resFunction, faultHandler));
}
private function faultHandler(fe:FaultHandler, token:Object=null):void{
Alert.show(fe.fault.faultString(), "Error in DataService");
}
}
You call the wrapped getItem method as follows. I really like it that in Flex 2 you do not need the Delegates anymore if you parameterize a function in a wrapper method, so calling these methods will be even easier:
private function getPerson(personId:int):void{
Services.getInstance().getItem(
Services.getInstance().dsPerson(),
{personId:personId},
new Person(),
personResHandler);
}
public function personResHandler(res:ResultEvent, token:Object=null):void{
var person:Person = Person(res.result);
Alert.show("Person retrieved is " + person.name);
}
Friday, May 4, 2007
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment