Skip to content

Commit

Permalink
Merging changes
Browse files Browse the repository at this point in the history
  • Loading branch information
danschultz committed Jun 24, 2011
2 parents b4fcbe5 + e09fd2c commit 93d7dda
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 9 deletions.
16 changes: 11 additions & 5 deletions src/mesh/model/Entity.as
Expand Up @@ -14,7 +14,6 @@ package mesh.model
import mesh.model.associations.HasOneDefinition;
import mesh.model.validators.Errors;
import mesh.model.validators.Validator;
import mesh.operations.Operation;
import mesh.services.DestroyRequest;
import mesh.services.Request;

Expand Down Expand Up @@ -133,13 +132,20 @@ package mesh.model
}

/**
* Reloads the attributes of this entity from the backend.
* Returns a request that will reload the attributes of this entity when executed.
*
* @return An executing operation.
* @return An unexecuted request.
*/
public function reload():Operation
public function reload():Request
{
return null;
var request:Request = Mesh.service(reflect.clazz).find(id);
request.addHandler({
success:function():void
{
translateFrom(request.translateTo());
}
});
return request;
}

/**
Expand Down
17 changes: 17 additions & 0 deletions src/mesh/model/associations/Association.as
Expand Up @@ -158,6 +158,23 @@ package mesh.model.associations
entity.markForRemoval();
}

/**
* Clears the association of its loaded data.
*/
public function reset():void
{
if (isLoading) {
_loadRequest.cancel();
_isLoading = false;
dispatchEvent( PropertyChangeEvent.createUpdateEvent(this, "isLoading", true, false) );
}

if (isLoaded) {
_isLoaded = false;
dispatchEvent( PropertyChangeEvent.createUpdateEvent(this, "isLoaded", true, false) );
}
}

/**
* Changes the state of the object for this association back to what it was at the last save.
*/
Expand Down
24 changes: 20 additions & 4 deletions src/mesh/model/associations/AssociationCollection.as
Expand Up @@ -33,7 +33,6 @@ package mesh.model.associations

afterLoad(loaded);
afterAdd(populateInverseAssociation);

afterAdd(registerObserversOnEntity);
}

Expand Down Expand Up @@ -84,6 +83,7 @@ package mesh.model.associations
for each (var property:Object in properties) {
var entity:Entity = new definition.target();
copy(property, entity);
populateInverseAssociation(entity);
result.push(entity);
}
return result.length == 1 ? result.pop() : result;
Expand Down Expand Up @@ -281,6 +281,22 @@ package mesh.model.associations
return object.removeItemAt(index);
}

/**
* @inheritDoc
*/
override public function reset():void
{
for each (var entity:Entity in toArray().concat(_removedEntities.toArray())) {
unregisterObserversOnEntity(entity);
}
object.removeEventListener(CollectionEvent.COLLECTION_CHANGE, handleEntitiesCollectionChange);

object = undefined;
_removedEntities.clear();

super.reset();
}

/**
* @inheritDoc
*/
Expand Down Expand Up @@ -337,8 +353,8 @@ package mesh.model.associations
*/
override flash_proxy function get dirtyEntities():Array
{
var result:Array = _removedEntities.toArray();
for each (var entity:Entity in this) {
var result:Array = [];
for each (var entity:Entity in toArray().concat(_removedEntities.toArray())) {
if (entity.isDirty) {
result.push(entity);
}
Expand Down Expand Up @@ -374,7 +390,7 @@ package mesh.model.associations
}

if (value != null && (!(value is Array) && !value.hasOwnProperty("toArray"))) {
throw new ArgumentError("AssociationCollection.object must be an Array, have a toArray method, or be null.");
throw new ArgumentError("AssociationCollection.object must be an Array, have a 'toArray' method, or be null.");
}

if (value != object) {
Expand Down
9 changes: 9 additions & 0 deletions src/mesh/model/associations/HasOneAssociation.as
Expand Up @@ -33,6 +33,15 @@ package mesh.model.associations
throw new IllegalOperationError("Cannot load " + this + " with undefined service undefined for " + definition);
}

/**
* @inheritDoc
*/
override public function reset():void
{
super.object = undefined;
super.reset();
}

/**
* @inheritDoc
*/
Expand Down
34 changes: 34 additions & 0 deletions src/mesh/services/Request.as
Expand Up @@ -4,6 +4,8 @@ package mesh.services

import mesh.core.proxy.DataProxy;

import mx.managers.CursorManager;

public dynamic class Request extends DataProxy
{
private var _block:Function;
Expand Down Expand Up @@ -33,23 +35,40 @@ package mesh.services
return this;
}

public function cancel():void
{

}

public function execute(handler:Object = null):Request
{
if (!_isExecuting) {
_isExecuting = true;

showBusyCursor();
if (handler != null) {
addHandler(handler);
}

executeBlock(_block);
}
return this;
}

private var _useBusyCursor:Boolean;
public function useBusyCursor():Request
{
_useBusyCursor = true;
return this;
}

protected function fault(fault:Object):void
{
hideBusyCursor();
for each (var handler:Object in _handlers) {
handler.fault(fault);
}
_isExecuting = false;
}

protected function result(data:Object):void
Expand All @@ -59,6 +78,7 @@ package mesh.services

protected function success():void
{
hideBusyCursor();
for each (var handler:Object in _handlers) {
handler.success();
}
Expand All @@ -84,6 +104,20 @@ package mesh.services
{
block.apply(null, blockArgs());
}

private function showBusyCursor():void
{
if (_useBusyCursor) {
CursorManager.setBusyCursor();
}
}

private function hideBusyCursor():void
{
if (_useBusyCursor) {
CursorManager.removeBusyCursor();
}
}
}
}

Expand Down
26 changes: 26 additions & 0 deletions src/mesh/services/SynchronousRequest.as
@@ -0,0 +1,26 @@
package mesh.services
{
import mesh.operations.MethodOperation;
import mesh.operations.Operation;

/**
* A request the executes a block function. The request's result will be the data
* that the block returns. If the block throws an error, the request will fault with
* that error.
*
* @author Dan Schultz
*/
public class SynchronousRequest extends OperationRequest
{
/**
* @copy Request#Request()
*/
public function SynchronousRequest(block:Function = null)
{
super(function():Operation
{
return new MethodOperation(block);
});
}
}
}
25 changes: 25 additions & 0 deletions tests/mesh/model/AssociationCollectionTests.as
Expand Up @@ -9,8 +9,12 @@ package mesh.model

import org.flexunit.assertThat;
import org.hamcrest.collection.array;
import org.hamcrest.collection.emptyArray;
import org.hamcrest.core.isA;
import org.hamcrest.object.equalTo;

use namespace flash_proxy;

public class AssociationCollectionTests
{
private var _collection:AssociationCollection;
Expand Down Expand Up @@ -41,6 +45,14 @@ package mesh.model
_collection.callback("afterLoad");
}

[Test]
public function testBuild():void
{
var order:Order = _collection.build({total:15});
assertThat(order.customer.flash_proxy::object, isA(Customer));
assertThat(order.total, equalTo(15));
}

[Test]
public function testRevertRestoresToOriginalEntities():void
{
Expand Down Expand Up @@ -100,5 +112,18 @@ package mesh.model
assertThat(order.isNew, equalTo(true));
assertThat(order.isDirty, equalTo(true));
}

[Test]
public function testReset():void
{
var order:Order = _collection.getItemAt(0) as Order;

_collection.reset();

assertThat(order.isMarkedForRemoval, equalTo(false));
assertThat(_collection.dirtyEntities, emptyArray());
assertThat(_collection.length, equalTo(0));
assertThat(_collection.isLoaded, equalTo(false));
}
}
}
35 changes: 35 additions & 0 deletions tests/mesh/model/EntityTests.as
@@ -0,0 +1,35 @@
package mesh.model
{
import mesh.Customer;
import mesh.Mesh;
import mesh.Name;
import mesh.services.Request;

import org.flexunit.assertThat;
import org.hamcrest.object.equalTo;

public class EntityTests
{
private var _customer:Customer;

[Before]
public function setup():void
{
_customer = new Customer();
_customer.name = new Name("Jimmy", "Page");
_customer.age = 67;
_customer.save().execute();
}

[Test]
public function testReload():void
{
var customer:Request = Mesh.service(Customer).find(_customer.id).execute();
customer.age = 68;
customer.reload().execute();

assertThat(customer.age, equalTo(_customer.age));
assertThat(customer.isDirty, equalTo(false));
}
}
}
19 changes: 19 additions & 0 deletions tests/mesh/model/HasOneAssociationTests.as
Expand Up @@ -9,6 +9,7 @@ package mesh.model

import org.flexunit.assertThat;
import org.hamcrest.object.equalTo;
import org.hamcrest.object.nullValue;

use namespace flash_proxy;

Expand Down Expand Up @@ -109,5 +110,23 @@ package mesh.model
customer.account.load().execute();
assertThat(customer.account.id, equalTo(account.id));
}

[Test]
public function testReset():void
{
var account:Account = new Account();
account.number = "000-001";

_customer.account.object = account;
_customer.account.save().execute();
_customer.save().execute();

var customer:Request = Mesh.service(Customer).find(_customer.id).execute();
customer.account.load().execute();

customer.account.reset();
assertThat(customer.account.object, nullValue());
assertThat(customer.account.isLoaded, equalTo(false));
}
}
}

0 comments on commit 93d7dda

Please sign in to comment.