Newer Version Available

This content describes an older version of this product. View Latest

Initializing Storage Service

To use storage, you must initialize it first and specify a name and, optionally, other properties. If you don't specify the optional properties, the Storage Service uses default values set by the initStorage() method of AuraStorageService.

You can initialize storage for your component using markup in one of two ways: by using a template or by adding the markup in the component body.

This example shows how to use a template to initialize storage using component markup. The component references the template in the template attribute. The template defined in the second example contains auraStorage:init tags that specify storage initialization properties. This example initializes three different storages: the framework-provided actions storage, and two custom storages named savings and checking.

1swfobject.registerObject("clippy.codeblock-0", "9");<aura:component render="client" template="auraStorageTest:namedStorageTemplate">
2</aura:component>
1swfobject.registerObject("clippy.codeblock-1", "9");<aura:component isTemplate="true" extends="aura:template">
2    <aura:set attribute="auraPreInitBlock">
3        <!-- Note that the maxSize attribute in <auraStorage:init> is in KB
4        <auraStorage:init name="actions" persistent="false" secure="false" maxSize="9999"/>
5        <auraStorage:init name="savings" persistent="false" secure="true" maxSize="6666"/>
6        <auraStorage:init name="checking" maxSize="7777"/>
7    </aura:set>    
8</aura:component>

Alternatively, you can add auraStorage:init tags directly in the body of your component definition. The following example shows component markup that initializes a storage named savings.

1swfobject.registerObject("clippy.codeblock-2", "9");<aura:component render="client" extensible="true" 
2 controller="java://org.auraframework.impl.java.controller.AuraStorageTestController" 
3 implements="auraStorage:refreshObserver">
4            
5    <auraStorage:init debugLoggingEnabled="true"
6                         name="savings" 
7                         secure="true" 
8                         persistent="false"
9                         clearStorageOnInit="true"
10                         defaultExpiration="50" 
11                         defaultAutoRefreshInterval="60" />
12</aura:component>

Alternatively, you can initialize storage on-the-fly using the JavaScript API. This example shows how to initialize the Storage Service using initStorage() in a JavaScript client-side controller.

1swfobject.registerObject("clippy.codeblock-3", "9");var storage = $A.storageService.initStorage("MyStorage",    // name
2                                            true,           // persistent
3                                            true,           // secure
4                                            524288,         // maxSize in bytes (512 * 1024)
5                                            600,            // defaultExpiration
6                                            600,            // defaultAutoRefreshInterval
7                                            true,           // debugLoggingEnabled
8                                            true);          // clearStorageOnInit

The maxSize parameter in $A.storageService.initStorage() has a unit of bytes. This is different than the maxSize attribute in <auraStorage:init>, which has a unit of KB.

Warning