State Management
State data should be saved into the browser session storage. This can be referenced from anywhere in your code.
Session storage is deliberately cleared when the user logs out or the browser is closed.
Accessing Session Storage
You can access the session storage using the following methods:
sessionStorage.getItem()
Description
This method will get an item for the specified key from the session storage.
Parameters
Parameter | Type | Description |
---|---|---|
key | string | The key of the item to get |
Return Value
The item
sessionStorage.setItem()
Description
This method will set an item using the specified key in the session storage.
Parameters
Parameter | Type | Description |
---|---|---|
key | string | The key of the item to set |
value | any | The item to set |
sessionStorage.removeItem()
Description
This method will remove an item with the specified key from the session storage.
Parameters
Parameter | Type | Description |
---|---|---|
key | string | The key of the item to remove |
Return Value
Boolean true
if the item was removed, false
if no item exists for the specified key
sessionStorage.clear()
Description
This method will clear all items from the session storage.
DataStoreHelper
Class (CSA Plugin)
The class DataStoreHelper
is available in the CSA plugin and makes it easy to access and manage data stored in the browser session storage:
class DataStoreHelper {
constructor() {}
/**
* Get an item from the session storage
* @param key The key for the item
* @return Item for the specified key
*/
getItem(key) {
return sessionStorage.getItem(key);
}
/**
* Set an item in the session storage
* @param key The key for the item
* @param value The value of the item
*/
setItem(key, value) {
sessionStorage.setItem(key, value);
}
/**
* Remove an item from the session storage
* @param key The key for the item
* @return Boolean true if the item was removed, false if no item was found for the specified key
*/
removeItem(key) {
return sessionStorage.removeItem(key);
}
/**
* Clear all data from the session storage
*/
clear() {
sessionStorage.clear();
}
}
The class DataStoreHelper
is available through the global variable dataStore
:
var dataStore = new DataStoreHelper;
This class can be implemented in other plugins by simply incorporating the above code.