Skip to main content

buildfire.localStorage

Shared Local Storage

LocalStorage works in isolated mode for plugins so that plugin instances don't share the same localStorage. This behavior is applied on the standard window.localStorage or using buildfire.localStorage.

important

LocalStorage is limited to 2k (2048) characters in length for the plugin instance as a whole. If more storage is required consider using File System.

tip

If you are trying to save data in order for it to be accessable on other plugins, consider using appData. It has more features, its scalable and persisted.

tip

It is recommended to use buildfire.localStorage instead of standard window.localStorage to have access to errors and to avoid browser incompatibilities in the future.

buildfire.localStorage

Acting very similar to window.localStorage. However, called synchronously with a callback and a return value when applicable.

getItem()

buildfire.localStorage.getItem(key, callback)

Retrieves the previously saved value under this key and returns the value inside the callback.

buildfire.localStorage.getItem("myItem", (error, value) => {
if (error) return console.error("something went wrong!", error);
if (value) {
console.log("myItem: ", value);
} else {
console.log("Nothing was previously saved");
}
});

key

NameTypeRequiredDescriptionDefault
keystringyesUnique identifier for your data

callback(err, value)

NameTypeDescription
errstringerror string, null when operation is successful
valuestringValue saved in localstorage

setItem()

buildfire.localStorage.setItem(key, value, callback)

This method will tell the app to save the value under the key and does not return a value.

buildfire.localStorage.setItem("myItem", "Hello World", (error) => {
if (error) return console.error("something went wrong!", error);
console.log("All is well, data saved and other plugins can now access it");
});

key

NameTypeRequiredDescriptionDefault
keystringyesUnique identifier for your data

value

NameTypeRequiredDescriptionDefault
valuestringyesValue to be saved in localStorage

callback(err)

NameTypeDescription
errstringerror string, null when operation is successful

removeItem()

buildfire.localStorage.removeItem(key, callback)

Removes the previously saved value under the key and does not return a value.

buildfire.localStorage.removeItem("myItem", (error) => {
if (error) return console.error("something went wrong!", error);

console.log("value removed");
});

key

NameTypeRequiredDescriptionDefault
keystringyesUnique identifier for your data

callback(err)

NameTypeDescription
errstringerror string, null when operation is successful

clear()

buildfire.localStorage.clear(callback)

Removes all plugin's localStorage keys and values. It does not return any value.

buildfire.localStorage.clear((error) => {
if (error) return console.error("something went wrong!", error);

console.log("localStorage cleared");
});

window.localStorage

getItem()

window.localStorage.getItem(key)

Gets the item from local storage

let myItem = window.localStorage.getItem("myItem");

setItem()

window.localStorage.setItem(key, value)

Asigns a new value to local storage item

window.localStorage.setItem("myItem", "newValue");

removeItem()

window.localStorage.removeItem(key)

Removes a value from local storage

window.localStorage.removeItem("myItem");

clear()

window.localStorage.clear()

Clears whole local storage

window.localStorage.clear();

note

Using indexed syntax [] is not supported and is not persisted in iOS. setItem and getItem should be used instead.