package <%= base_package %>.models.domain { import flash.events.Event; import flash.events.EventDispatcher; import mx.binding.utils.BindingUtils; import <%= base_package %>.models.constant.DocumentType; import <%= base_package %>.models.domain.Content; [Bindable] public class Document extends EventDispatcher { /** Document isn't a RestfulX Model, but it acts like one, using Content as it's data */ public var content:Content; public var desiredProperties:Array; public var documentType:DocumentType; public function Document() { BindingUtils.bindSetter(dispatchChange, this, ["content", "title"]); BindingUtils.bindSetter(dispatchChange, this, ["content", "body"]); BindingUtils.bindSetter(dispatchChange, this, ["content", "abstract"]); BindingUtils.bindSetter(dispatchChange, this, ["content", "menuTitle"]); BindingUtils.bindSetter(dispatchChange, this, ["content", "subTitle"]); desiredProperties = ["title", "body", "abstract", "menuTitle", "subTitle"]; content = new Content(); } public function mapProperties(source:Object, target:Object, properties:*):void { for each (var property:* in properties) { target[property] = source[property]; } } protected function dispatchChange(property:*=null):void { dispatchEvent(new Event(Event.CHANGE)); } /** * Create a snapshot of the current state of the document. This snapshot can * be used to restore the document at a later time, for example when undoing * a series of actions. To restore the document pass the snapshot to loadSnapshot. */ public function createSnapshot():Snapshot { var snapshot:DocumentSnapshot = new DocumentSnapshot(); mapProperties(snapshot, content, desiredProperties); /*snapshot.title = content.title; snapshot.body = content.body; snapshot.abstract = content.abstract; snapshot.menuTitle = content.menuTitle; snapshot.subTitle = content.subTitle;*/ snapshot.type = documentType; return snapshot; } /** * Restore the document as it was at an earlier time. See createSnapshot. */ public function loadSnapshot(snapshot:Snapshot):void { loadSnapshotImpl(snapshot); } /** * This is the actual implementation of loadSnapshot. Because there are problems * with overriding methods in custom namespaces this method is needed to * let subclasses override the loading of snapshots. */ protected function loadSnapshotImpl(snapshot:Snapshot) : void { if (snapshot is DocumentSnapshot) { var s:DocumentSnapshot = DocumentSnapshot(snapshot); mapProperties(content, s, desiredProperties); } else { throw new ArgumentError("Unsupported snapshot type"); } } } } import flash.utils.ByteArray; import <%= base_package %>.models.constant.DocumentType; import <%= base_package %>.models.domain.Snapshot; /** * The document snapshot implementation. It is based on the DocumentData VO and adds the * type and the getters defined by the Snapshot interface. */ class DocumentSnapshot implements Snapshot { public var type:DocumentType; public var title:String; public var abstract:String; public var menuTitle:String; public var subTitle:String; public var body:String; public function get bytes():ByteArray { var data:ByteArray = new ByteArray(); data.writeObject(this); return data; } public function get xml():XML { return {title} {body} {abstract} {menuTitle} {subTitle} ; } public function DocumentSnapshot() { } }