diff --git a/src/mesh/services/ItemQueryRequest.as b/src/mesh/services/ItemQueryRequest.as new file mode 100644 index 0000000..e14f8c2 --- /dev/null +++ b/src/mesh/services/ItemQueryRequest.as @@ -0,0 +1,22 @@ +package mesh.services +{ + import flash.utils.flash_proxy; + + use namespace flash_proxy; + + public dynamic class ItemQueryRequest extends QueryRequest + { + public function ItemQueryRequest(service:Service, deserializer:Function, block:Function) + { + super(service, deserializer, block); + } + + override flash_proxy function set object(value:*):void + { + if (value is Array) { + value = value[0]; + } + super.object = value; + } + } +} \ No newline at end of file diff --git a/src/mesh/services/ListQueryRequest.as b/src/mesh/services/ListQueryRequest.as index cff1a67..8c882b7 100644 --- a/src/mesh/services/ListQueryRequest.as +++ b/src/mesh/services/ListQueryRequest.as @@ -12,7 +12,7 @@ package mesh.services { private var _list:ArrayList; - public function ListQueryRequest(service:Service, block:Function) + public function ListQueryRequest(service:Service, deserializer:Function, block:Function) { _list = new ArrayList(); _list.addEventListener(CollectionEvent.COLLECTION_CHANGE, function(event:CollectionEvent):void @@ -20,7 +20,7 @@ package mesh.services dispatchEvent(event); }); - super(service, block); + super(service, deserializer, block); } /** @@ -36,7 +36,7 @@ package mesh.services */ public function addItemAt(item:Object, index:int):void { - flash_proxy::object.addItemAt(item, index); + object.addItemAt(item, index); } /** @@ -56,7 +56,7 @@ package mesh.services */ public function getItemAt(index:int, prefetch:int = 0):Object { - return flash_proxy::object.getItemAt(index, prefetch); + return object.getItemAt(index, prefetch); } /** @@ -64,7 +64,7 @@ package mesh.services */ public function getItemIndex(item:Object):int { - return flash_proxy::object.getItemIndex(item); + return object.getItemIndex(item); } /** @@ -72,7 +72,7 @@ package mesh.services */ public function itemUpdated(item:Object, property:Object = null, oldValue:Object = null, newValue:Object = null):void { - flash_proxy::object.itemUpdated(item, property, oldValue, newValue); + object.itemUpdated(item, property, oldValue, newValue); } /** @@ -110,7 +110,7 @@ package mesh.services */ public function removeAll():void { - flash_proxy::object.removeAll(); + object.removeAll(); } /** @@ -132,7 +132,7 @@ package mesh.services */ public function removeItemAt(index:int):Object { - return flash_proxy::object.removeItemAt(index); + return object.removeItemAt(index); } /** @@ -140,7 +140,7 @@ package mesh.services */ public function setItemAt(item:Object, index:int):Object { - return flash_proxy::object.setItemAt(item, index); + return object.setItemAt(item, index); } /** @@ -148,7 +148,7 @@ package mesh.services */ public function toArray():Array { - return flash_proxy::object.toArray(); + return object.toArray(); } /** @@ -156,7 +156,7 @@ package mesh.services */ public function get length():int { - return flash_proxy::object.length; + return object.length; } /** diff --git a/src/mesh/services/QueryRequest.as b/src/mesh/services/QueryRequest.as index a040a7a..c9abf92 100644 --- a/src/mesh/services/QueryRequest.as +++ b/src/mesh/services/QueryRequest.as @@ -1,18 +1,25 @@ package mesh.services { - public dynamic class QueryRequest extends OperationRequest + import mesh.core.array.flatten; + + public dynamic class QueryRequest extends ServiceRequest { - private var _service:Service; + private var _deserializer:Function; - public function QueryRequest(service:Service, block:Function) + public function QueryRequest(service:Service, deserializer:Function, block:Function) { - super(block); - _service = service; + super(service, block); + _deserializer = deserializer; } override protected function result(data:Object):void { - _service.register(data); + if (data.hasOwnProperty("toArray")) { + data = data.toArray(); + } + + data = _deserializer(flatten(data)); + service.register(data); super.result(data); } } diff --git a/src/mesh/services/Service.as b/src/mesh/services/Service.as index 88dc708..e85f878 100644 --- a/src/mesh/services/Service.as +++ b/src/mesh/services/Service.as @@ -36,23 +36,29 @@ package mesh.services * @return An unexecuted operation. */ public function all():ListQueryRequest + { + return new ListQueryRequest(this, deserialize, function():Operation + { + return createAllOperation(); + }); + } + + protected function createAllOperation():Operation { throw new IllegalOperationError(reflect.name + " does not support retrieval of entities using all()"); } public function belongingTo(entity:Entity):ListQueryRequest { - throw new IllegalOperationError(reflect.name + " does not support retrieval of entities belonging to " + entity); + return new ListQueryRequest(this, deserialize, function():Operation + { + return createBelongingToOperation(entity); + }); } - public function create(clazz:Class):* + protected function createBelongingToOperation(entity:Entity):Operation { - if (clazz is Entity) { - var instance:Entity = new clazz(); - register(instance); - return instance; - } - throw new ArgumentError("Expected class to be an Entity"); + throw new IllegalOperationError(reflect.name + " does not support retrieval of entities belonging to " + entity); } /** @@ -115,12 +121,28 @@ package mesh.services return ids.length == 1 ? findOne(ids[0]) : findMany(ids); } - public function findOne(id:*):QueryRequest + public function findOne(id:*):ItemQueryRequest + { + return new ItemQueryRequest(this, deserialize, function():Operation + { + return createFindOneOperation(id); + }); + } + + protected function createFindOneOperation(id:*):Operation { throw new IllegalOperationError(reflect.name + " does not support retrieval of entities using findOne()."); } public function findMany(...ids):ListQueryRequest + { + return new ListQueryRequest(this, deserialize, function():Operation + { + return createFindManyOperation.apply(null, ids); + }); + } + + protected function createFindManyOperation(...ids):Operation { throw new IllegalOperationError(reflect.name + " does not support retrieval of entities using findMany()."); } diff --git a/src/mesh/services/ServiceRequest.as b/src/mesh/services/ServiceRequest.as new file mode 100644 index 0000000..a8e08bd --- /dev/null +++ b/src/mesh/services/ServiceRequest.as @@ -0,0 +1,17 @@ +package mesh.services +{ + public class ServiceRequest extends OperationRequest + { + public function ServiceRequest(service:Service, block:Function) + { + super(block); + _service = service; + } + + private var _service:Service; + protected function get service():Service + { + return _service; + } + } +} \ No newline at end of file diff --git a/src/mesh/services/TestService.as b/src/mesh/services/TestService.as index 5866baf..c9b509e 100644 --- a/src/mesh/services/TestService.as +++ b/src/mesh/services/TestService.as @@ -23,48 +23,38 @@ package mesh.services _belongingToBlock = belongingToBlock; } - override public function belongingTo(entity:Entity):ListQueryRequest + override protected function createBelongingToOperation(entity:Entity):Operation { - return new ListQueryRequest(this, function():Operation + return createOperation(function():Object { - return createOperation(function():Object - { - return _belongingToBlock == null ? [] : deserialize(_belongingToBlock(entity, _registry.values())); - }); + return _belongingToBlock == null ? [] : _belongingToBlock(entity, _registry.values()); }); } - override public function findOne(id:*):QueryRequest + override protected function createFindOneOperation(id:*):Operation { - return new QueryRequest(this, function():Operation + return createOperation(function():Object { - return createOperation(function():Object - { - if (_registry.containsKey(id)) { - var entities:Array = deserialize([_registry.grab(id)]); - return entities[0]; - } - throw new ArgumentError("Entity not found with ID=" + id); - }); + if (_registry.containsKey(id)) { + return _registry.grab(id); + } + throw new ArgumentError("Entity not found with ID=" + id); }); } - override public function findMany(...ids):ListQueryRequest + override protected function createFindManyOperation(...ids):Operation { - return new ListQueryRequest(this, function():Operation + return createOperation(function():Object { - return createOperation(function():Object - { - var objects:Array = []; - for each (var id:int in ids) { - if (_registry.containsKey(id)) { - objects.push(_registry.grab(id)); - } else { - throw new ArgumentError("Entity not found with ID=" + id); - } + var objects:Array = []; + for each (var id:int in ids) { + if (_registry.containsKey(id)) { + objects.push(_registry.grab(id)); + } else { + throw new ArgumentError("Entity not found with ID=" + id); } - return deserialize(objects); - }); + } + return objects; }); } diff --git a/src/mesh/services/WhereQueryRequest.as b/src/mesh/services/WhereQueryRequest.as index dbf7ddb..1bff7e9 100644 --- a/src/mesh/services/WhereQueryRequest.as +++ b/src/mesh/services/WhereQueryRequest.as @@ -2,9 +2,9 @@ package mesh.services { public dynamic class WhereQueryRequest extends ListQueryRequest { - public function WhereQueryRequest(service:Service, block:Function) + public function WhereQueryRequest(service:Service, deserializer:Function, block:Function) { - super(service, block); + super(service, deserializer, block); } } } \ No newline at end of file