Compaction is an unobtrusive Flex library designed to make creating forms easier.
Take a standard mxml form.
<mx:Form id="form" width="100%">
<mx:FormItem label="Name">
<mx:TextInput id="nameInput" />
</mx:FormItem>
<mx:FormItem direction="horizontal">
<mx:Button id="saveButton" label="Save" />
<mx:Button id="cancelButton" label="Cancel"/>
</mx:FormItem>
</mx:Form>
Drop in the compaction edit model.
<model:EditModel id="model" />
<mx:Form id="form" width="100%">
<mx:FormItem label="Name">
<mx:TextInput id="nameInput" />
</mx:FormItem>
<mx:FormItem direction="horizontal">
<mx:Button id="saveButton" label="Save" />
<mx:Button id="cancelButton" label="Cancel"/>
</mx:FormItem>
</mx:Form>
And bind your text input to the model.
<model:EditModel id="model" />
<mx:Form id="form" width="100%">
<mx:FormItem label="Name">
<mx:TextInput id="nameInput" />
</mx:FormItem>
<mx:FormItem direction="horizontal">
<mx:Button id="saveButton" label="Save" />
<mx:Button id="cancelButton" label="Cancel"/>
</mx:FormItem>
</mx:Form>
<binder:TextBinder source="{model.edited}" property="name" target="{nameInput}" />
This will create a two way binding between your text input and the compaction edit model.
model.edit.execute(new Customer());
model.changed.currentValue;
<binder:ActionBinder source="{model.cancel}" target="{cancelButton}" />
<binder:ActionBinder source="{model.save}" target="{saveButton}" />
<binder:ConditionBinder source="{model.editing}" target="{form}" />
This is done automatically by the action binder shown in steps 3 and 4.
First create a validator.
public class CustomerValidator implements IValidator {
public function validate(item:Object, builder:IValidationBuilder): void {
var customer:Customer = Customer(item);
builder.string(customer.name, "name").notEmpty();
}
}
Then assign it to the edit model.
<validator:CustomerValidator id="validator" />
<model:EditModel id="model" validator="{validator}"/>
This is done automatically by the action binder shown in step 3.
<binder:ValidationBinder source="{model.adapter}" target="{nameInput}" />
<validator:CustomerValidator id="validator" />
<model:EditModel id="model" validator="{validator}"/>
<mx:Form id="form" width="100%">
<mx:FormItem label="Name">
<mx:TextInput id="nameInput" />
</mx:FormItem>
<mx:FormItem direction="horizontal">
<mx:Button id="saveButton" label="Save" />
<mx:Button id="cancelButton" label="Cancel"/>
</mx:FormItem>
</mx:Form>
<binder:FormBinder source="{model}" target="{form}" />