-
Notifications
You must be signed in to change notification settings - Fork 2
Change Tracking
In order for Mesh to know which models need to be persisted, it needs to keep track of how your application has modified them. Mesh will automatically track changes to properties that are marked as [Bindable]
. This also includes any properties or code segments that dispatch PropertyChangeEvent
's.
public class Order extends Entity
{
[Bindable]
public var shippingAddress:Address;
}
You can also mark a property as dirty by using the propertyChanged()
method.
public class Order extends Entity
{
public function Order()
{
super();
}
public function addOrderItem(item:OrderItem):void
{
var oldTotal:Number = total;
orderItems.addItem(item);
propertyChanged("total", oldTotal, total);
}
public function get total():Number
{
var result:Number = 0;
for each (var item:OrderItem in orderItems) {
result += item.amount;
}
return result;
}
}
Note: This behavior is generally discouraged, since your views are probably already leveraging data-binding. Instead of calling propertyChanged
, it's more elegant to dispatch a PropertyChangeEvent
manually and have Mesh handle it. However, it does serve a use-case when it's necessary to mark a property as dirty, and not have your views reflect the change.
Mesh will track the models that are added and removed from your associations. When a model is saved, Mesh will send insert requests to your backend for new instances, and send destroy requests for removed instances.
Reverting is useful when a save fails, and you need to return your application to a state before any changes were made.
Models and associations can be reverted by calling their revert()
method. Reverting a model or association will return it to its state before its last save.
var customer:Customer = new Customer({name:"Jimmy Page", age:67});
customer.orders.addItem(new Order());
customer.save();
customer.orders.save();
customer.age = 67;
customer.orders.removeItemAt(0);
customer.revert();
customer.orders.revert();
trace(customer.age); // 67
trace(customer.orders.length); // 1
You can hook into Mesh's change tracking and query the previous value for a property. This can be achieved by calling Entity.whatWas()
.
var customer:Customer = new Customer();
customer.age = 25;
customer.save()
customer.age = 26;
trace(customer.whatWas("age")); // 25