Skip to content

Commit

Permalink
adding observers to entity
Browse files Browse the repository at this point in the history
  • Loading branch information
danschultz committed Apr 14, 2011
1 parent 468570d commit c6d5f65
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 45 deletions.
41 changes: 24 additions & 17 deletions src/mesh/Callbacks.as
@@ -1,12 +1,9 @@
package mesh
{
import collections.ArrayList;
import collections.ArraySequence;
import collections.HashMap;

import flash.utils.Proxy;
import flash.utils.flash_proxy;

public class Callbacks extends Proxy
public class Callbacks
{
private var _callbacks:HashMap = new HashMap();

Expand All @@ -17,7 +14,7 @@ package mesh

public function addCallback(method:String, block:Function):void
{
callbacks(method).add(new Callback(block));
callbacks(method).add( new Callback(method, block) );
}

public function callback(method:String, ...args):void
Expand All @@ -27,23 +24,22 @@ package mesh
}
}

private function callbacks(method:String):ArrayList
private function callbacks(method:String):ArraySequence
{
if (!_callbacks.containsKey(method)) {
_callbacks.put(method, new ArrayList());
_callbacks.put(method, new ArraySequence());
}
return _callbacks.grab(method);
}

/**
* @inheritDoc
*/
override flash_proxy function callProperty(name:*, ...parameters):*
public function removeCallback(method:String, block:Function):void
{
name = name.toString();

if ((name.length > 6 && name.indexOf("before") == 0) || (name.length > 5 && name.indexOf("after") == 0)) {
callback(name);
var callbacks:ArraySequence = callbacks(method)
for each (var callback:Callback in callbacks) {
if (callback.block == block) {
callbacks.remove(callback);
break;
}
}
}
}
Expand All @@ -52,8 +48,9 @@ package mesh
class Callback
{
private var _block:Function;
private var _method:String;

public function Callback(block:Function)
public function Callback(method:String, block:Function)
{
_block = block;
}
Expand All @@ -62,4 +59,14 @@ class Callback
{
_block.apply(null, args);
}

public function get method():String
{
return _method;
}

public function get block():Function
{
return _block;
}
}
27 changes: 19 additions & 8 deletions src/mesh/Entity.as
Expand Up @@ -4,7 +4,6 @@ package mesh
import flash.events.EventDispatcher;

import mesh.associations.Association;
import mesh.associations.AssociationDefinition;
import mesh.associations.HasManyAssociation;
import mesh.associations.HasManyDefinition;
import mesh.associations.HasOneAssociation;
Expand All @@ -29,16 +28,17 @@ package mesh
public class Entity extends EventDispatcher
{
private var _callbacks:Callbacks = new Callbacks();
private var _observers:Callbacks = new Callbacks();
private var _associations:Object = {};
private var _changes:Properties = new Properties(this);

/**
* Constructor.
*/
public function Entity(properties:Object = null)
public function Entity(values:Object = null)
{
super();
copy(properties, this);
copy(values, this);

// add necessary callbacks for find
afterFind(markNonLazyAssociationsAsLoaded);
Expand All @@ -54,6 +54,16 @@ package mesh
addEventListener(PropertyChangeEvent.PROPERTY_CHANGE, handlePropertyChange);
}

public function addObserver(method:String, block:Function):void
{
_observers.addCallback(method, block);
}

public function removeObserver(method:String, block:Function):void
{
_observers.removeCallback(method, block);
}

/**
* Returns an association proxy for the given the given property. The proxy that is
* returned is determined by the relationship type. For instance, if the property is
Expand Down Expand Up @@ -94,6 +104,7 @@ package mesh
public function callback(method:String):void
{
_callbacks.callback(method);
_observers.callback(method, this);
}

private function addCallback(method:String, block:Function):void
Expand Down Expand Up @@ -134,7 +145,7 @@ package mesh
return service.destroy(this);
}

protected function beforeDestroy(block:Function):void
public function beforeDestroy(block:Function):void
{
addCallback("beforeDestroy", block);
}
Expand All @@ -145,17 +156,17 @@ package mesh
*
* @param block The callback function.
*/
protected function beforeSave(block:Function):void
public function beforeSave(block:Function):void
{
addCallback("beforeSave", block);
}

protected function afterDestroy(block:Function):void
public function afterDestroy(block:Function):void
{
addCallback("afterDestroy", block)
}

protected function afterFind(block:Function):void
public function afterFind(block:Function):void
{
addCallback("afterFind", block);
}
Expand All @@ -165,7 +176,7 @@ package mesh
*
* @param block The callback function.
*/
protected function afterSave(block:Function):void
public function afterSave(block:Function):void
{
addCallback("afterSave", block);
}
Expand Down
19 changes: 10 additions & 9 deletions src/mesh/associations/AssociationCollection.as
Expand Up @@ -33,15 +33,7 @@ package mesh.associations
{
super(source, relationship);
flash_proxy::object = [];

afterLoad(function(proxy:AssociationCollection):void
{
_originalEntities = new ArraySequence(toArray());

for each (var entity:Entity in proxy) {
entity.callback("afterFind");
}
});
afterLoad(loaded);
}

/**
Expand Down Expand Up @@ -171,6 +163,15 @@ package mesh.associations
flash_proxy::object.itemUpdated(item, property, oldValue, newValue);
}

private function loaded():void
{
_originalEntities = new ArraySequence(toArray());

for each (var entity:Entity in this) {
entity.callback("afterFind");
}
}

/**
* @copy #removeItem()
*/
Expand Down
8 changes: 4 additions & 4 deletions src/mesh/associations/HasAssociation.as
Expand Up @@ -61,17 +61,17 @@ package mesh.associations
override flash_proxy function set object(value:*):void
{
if (flash_proxy::object != null) {
flash_proxy::object.removeCallback(targetDestroyed);
flash_proxy::object.removeCallback(targetSaved);
flash_proxy::object.removeObserver("afterDestroy", targetDestroyed);
flash_proxy::object.removeObserver("afterSave", targetSaved);
}

super.flash_proxy::object = value;

if (flash_proxy::object != null) {
flash_proxy::object.revive();

flash_proxy::object.afterSave(targetSaved);
flash_proxy::object.afterDestroy(targetDestroyed);
flash_proxy::object.addObserver("afterSave", targetSaved);
flash_proxy::object.addObserver("afterDestroy", targetDestroyed);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/mesh/services/QueryRequest.as
Expand Up @@ -4,7 +4,7 @@ package mesh.services
{
private var _service:Service;

public function QueryRequest(block:Function)
public function QueryRequest(service:Service, block:Function)
{
super(block);
_service = service;
Expand Down
2 changes: 1 addition & 1 deletion src/mesh/services/RPCServiceAdaptor.as
Expand Up @@ -47,7 +47,7 @@ package mesh.services
*
* @inheritDoc
*/
public function createOperation(...args):Operation
override public function createOperation(...args):Operation
{
return newInstance.apply(null, [ServiceOperation, service].concat(args));
}
Expand Down
5 changes: 0 additions & 5 deletions src/mesh/services/Service.as
Expand Up @@ -48,11 +48,6 @@ package mesh.services
throw new ArgumentError("Expected class to be an Entity");
}

protected function createQuery(block:Function):QueryRequest
{
return new QueryRequest(block);
}

/**
* Marks the given entities to be destroyed the next time this service is saved.
*
Expand Down

0 comments on commit c6d5f65

Please sign in to comment.