Skip to content

Commit

Permalink
Dispatch load events from entity
Browse files Browse the repository at this point in the history
  • Loading branch information
danschultz committed Sep 1, 2011
1 parent 546bf1f commit dcce70a
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 10 deletions.
50 changes: 42 additions & 8 deletions src/mesh/model/Entity.as
Expand Up @@ -4,6 +4,7 @@ package mesh.model
import com.brokenfunction.json.encodeJson;

import flash.errors.IllegalOperationError;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.utils.flash_proxy;

Expand All @@ -14,7 +15,11 @@ package mesh.model
import mesh.model.associations.Association;
import mesh.model.associations.HasManyAssociation;
import mesh.model.associations.HasOneAssociation;
import mesh.model.load.LoadEvent;
import mesh.model.load.LoadFailedEvent;
import mesh.model.load.LoadSuccessEvent;
import mesh.model.serialization.Serializer;
import mesh.model.source.SourceFault;
import mesh.model.store.Store;
import mesh.model.validators.Errors;
import mesh.model.validators.Validator;
Expand All @@ -24,6 +29,21 @@ package mesh.model

use namespace flash_proxy;

/**
* Dispatched when the result list starts loading its content.
*/
[Event(name="loading", type="mesh.model.load.LoadEvent")]

/**
* Dispatched when the result list has successfully loaded its content.
*/
[Event(name="success", type="mesh.model.load.LoadSuccessEvent")]

/**
* Dispatched when the result list has failed to load its content.
*/
[Event(name="failed", type="mesh.model.load.LoadFailedEvent")]

/**
* An entity.
*
Expand All @@ -47,10 +67,7 @@ package mesh.model
_aggregates = new Aggregates(this);

_status = new EntityStatus(this);
_status.addEventListener(StateEvent.ENTER, function(event:StateEvent):void
{
dispatchEvent(event.clone());
});
_status.addEventListener(StateEvent.ENTER, handleStatusChange);

copy(values, this);
addEventListener(PropertyChangeEvent.PROPERTY_CHANGE, handlePropertyChange);
Expand Down Expand Up @@ -111,15 +128,15 @@ package mesh.model
return this;
}

private var _fault:SourceFault;
/**
* Puts the entity into an errored lifecycle state. This state signifies that an error
* occurred after a load or commit.
*
* @return This instance.
*/
public function errored():Entity
public function failed(fault:SourceFault):void
{
throw new IllegalOperationError("Entity.errored() is not implemented.");
_fault = fault;
status.failed();
}

/**
Expand Down Expand Up @@ -159,6 +176,23 @@ package mesh.model
propertyChanged(event.property.toString(), event.oldValue, event.newValue);
}

private function handleStatusChange(event:StateEvent):void
{
dispatchEvent(event.clone());

if (status.isErrored) {
dispatchEvent( new LoadFailedEvent(LoadFailedEvent.FAILED, _fault) );
}

if (status.isSynced) {
dispatchEvent( new LoadSuccessEvent(LoadSuccessEvent.SUCCESS) );
}

if (status.isBusy) {
dispatchEvent( new LoadEvent(LoadEvent.LOADING) );
}
}

/**
* Returns a generated hash value for this entity. Two entities that represent
* the same data should return the same hash code.
Expand Down
18 changes: 18 additions & 0 deletions src/mesh/model/EntityStatus.as
Expand Up @@ -28,6 +28,7 @@ package mesh.model
private var _state:StateMachine;
private var _loading:Action;
private var _destroy:Action;
private var _failed:Action;
private var _dirty:Action;
private var _synced:Action;

Expand Down Expand Up @@ -74,6 +75,14 @@ package mesh.model
_destroy.trigger();
}

/**
* Puts the entity int a failed state.
*/
public function failed():void
{
_failed.trigger();
}

private function isInState(state:String):Boolean
{
return _state.current.name.search(state) != -1;
Expand Down Expand Up @@ -103,6 +112,8 @@ package mesh.model

var loadingBusyState:State = _state.createState("loading busy");

var failedState:State = _state.createState("failed");

var persistedState:State = _state.createState("persisted").onEnter(_entity.changes.clear);
var persistedDirtyState:State = _state.createState("persisted dirty");

Expand All @@ -113,6 +124,7 @@ package mesh.model
_dirty = _state.createAction("dirty").transitionTo(persistedDirtyState, persistedState);

_destroy = _state.createAction("destroy").transitionTo(destroyedDirtyState, [persistedState, persistedDirtyState]);
_failed = _state.createAction("failed").transitionTo(failedState, loadingBusyState);

_synced = _state.createAction("synced");
_synced.transitionTo(persistedState, [loadingBusyState, newDirtyState, persistedDirtyState]);
Expand All @@ -137,6 +149,12 @@ package mesh.model
return isInState("destroy");
}

[Bindable(event="enter")]
public function get isErrored():Boolean
{
return isInState("failed");
}

[Bindable(event="enter")]
private function get isLocked():Boolean
{
Expand Down
2 changes: 1 addition & 1 deletion src/mesh/model/source/FixtureSource.as
Expand Up @@ -111,7 +111,7 @@ package mesh.model.source
entity.fromObject(_fixtures[data.id]);
entity.synced();
} else {
entity.errored();
entity.failed();
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/mesh/model/store/Commit.as
Expand Up @@ -158,7 +158,7 @@ package mesh.model.store
_operation.failed(fault);

for each (var entity:Entity in storeEntities(entities)) {
entity.errored();
entity.failed();
}
}

Expand Down

0 comments on commit dcce70a

Please sign in to comment.