.models.<%= class_name %>;
<% for model in belongs_tos -%>
import <%= base_package %>.models.<%= model.camelcase %>;
<% end -%>
<% if attachment_field.size > 0 -%>
import flash.net.FileReference;
import org.restfulx.utils.RxFileReference;
<% end -%>
[Bindable]
private var <%= class_name.dcfirst %>:<%= class_name %> = new <%= class_name %>();
<% if attachment_field.size > 0 -%>
[Bindable]
private var fileName:String = "None selected";
private var file:RxFileReference;
<% end -%>
private function new<%= class_name %>():void {
<%= class_name.dcfirst %> = new <%= class_name %>();
<%= class_name.dcfirst.pluralize %>List.selectedIndex = -1;
}
private function save<%= class_name %>():void {
updateModelProperties();
if (<%= class_name.dcfirst %>.id) {
<%= class_name.dcfirst %>.update({onSuccess: on<%= class_name %>Update});
} else {
<%= class_name.dcfirst %>.create({onSuccess: on<%= class_name %>Create});
}
}
private function updateModelProperties():void {
<% for attribute in attributes -%>
<% if attribute.type == :boolean -%>
<%= class_name.dcfirst %>.<%= attribute.flex_name %> = <%= attribute.flex_name %>CheckBox.selected;
<% elsif attribute.type == :string -%>
<%= class_name.dcfirst %>.<%= attribute.flex_name %> = <%= attribute.flex_name %>TextInput.text;
<% elsif attribute.type == :text -%>
<%= class_name.dcfirst %>.<%= attribute.flex_name %> = <%= attribute.flex_name %>TextArea.text;
<% elsif attribute.type == :datetime || attribute.type == :time -%>
<%= class_name.dcfirst %>.<%= attribute.flex_name %> = <%= attribute.flex_name %>DateTimeTextInput.date;
<% elsif attribute.type == :date -%>
<%= class_name.dcfirst %>.<%= attribute.flex_name %> = <%= attribute.flex_name %>DateField.selectedDate;
<% else -%>
<%= class_name.dcfirst %>.<%= attribute.flex_name %> = <%= attribute.flex_type %>(<%= attribute.flex_name %>TextInput.text);
<% end -%>
<% end -%>
<% for model in belongs_tos -%>
<%= class_name.dcfirst %>.<%= model.camelcase(:lower) %> = <%= model.camelcase %>(<%= model.camelcase(:lower) %>ComboBox.selectedItem);
<% end -%>
<% if attachment_field.size > 0 -%>
<%= class_name.dcfirst %>.attachment = file;
<% end -%>
}
private function destroy<%= class_name %>():void {
<%= class_name.dcfirst %>.destroy({onSuccess: on<%= class_name %>Destroy});
}
private function on<%= class_name %>Select():void {
<%= class_name.dcfirst %> = RxUtils.clone(<%= class_name.dcfirst.pluralize %>List.selectedItem) as <%= class_name %>;
}
private function on<%= class_name %>Create(result:<%= class_name %>):void {
<%= class_name.dcfirst %> = new <%= class_name %>;
}
private function on<%= class_name %>Update(result:<%= class_name %>):void {
<%= class_name.dcfirst.pluralize %>List.selectedItem = result;
on<%= class_name %>Select();
}
private function on<%= class_name %>Destroy(result:<%= class_name %>):void {
on<%= class_name %>Create(result);
}
<% if attachment_field.size > 0 -%>
private function chooseFile():void {
file = new RxFileReference("<%= attachment_field[0].camelcase(:lower) %>");
file.reference.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler, false, 0, true);
file.reference.addEventListener(Event.SELECT, selectFile, false, 0, true);
file.reference.addEventListener(Event.CANCEL, cancelBrowse, false, 0, true);
file.reference.browse();
}
private function selectFile(event:Event):void {
fileSelected(event)
}
private function cancelBrowse(event:Event):void {
file = null;
}
private function fileSelected(event:Event):void {
fileName = FileReference(event.target).name;
}
private function ioErrorHandler(event:Event):void {
fileChooser.errorString = "Failed to selected a file. Please try again.";
}
<% end -%>
]]>
<% for attribute in attributes -%>
<% if attribute.type == :boolean -%>
<% elsif attribute.type == :string -%>
<% elsif attribute.type == :text -%>
<% elsif attribute.type == :datetime || attribute.type == :time -%>
<% elsif attribute.type == :date -%>
<% else -%>
<% end -%>
<% end -%>
<% if attachment_field.size > 0 -%>
<% end -%>
<% for model in belongs_tos -%>
<% end -%>