Client Persistence

Learn about how to persist data in the client using Crux

Crux provides a bean to help with client persistence: AMFStorageBean. This bean the AMFStorage clasx under the hood that uses AMF to encode/decode data in browser’s local storage.

Using AMF simplifies development since you don’t need to code additional methods for encode and decode your objects from/to the browser local storage, also it provides strong typing. Using JSON instead, will need more work from your side, while AMF does the heavy duty for you under the hood.

AMFStorageBean

To use the AMFStorageBean, you simply declare it in a BeanProvider:

<crux:BeanProvider
    xmlns:fx="http://ns.adobe.com/mxml/2009"
    xmlns:crux="library://ns.apache.org/royale/crux">
  
    <crux:AMFStorageBean id="amfStorageBean" name="todomvc" localPath="crux"/>
 
</crux:BeanProvider>

Inject the instance into your model and declare a bindable getter/setter:

[Inject]
public var so:IAMFStorageBean;
  
[Bindable]
public function get appIndex():int
{
    // the second parameter is the initial value
    return so.getInt("appIndex", 0);
}
  
public function set appIndex(index:int):void
{
    so.setInt("appIndex", index);
}