QScript DocumentStorage

From Q
Jump to navigation Jump to search

DocumentStorage

Works like the browser's window.localStorage, but is saved with the document and limited to 5 MB.

equals(obj)

Whether two objects are the same.
objThe object to compare against.
Returns:true or false
Example:
data_file.getQuestionByName('Q2').equals(data_file.questions()[0])

getItem(key)

Returns the value stored under key, or null if there is none. Values aren't kept in sync with the document, so if a value refers to an item, such as by its GUID, check the item still exists before relying on it.
keyThe key to look up.
Example:
var status = project.documentStorage.getItem('Q-data-cleaning-status-' + file.guid);

key(index)

Returns the key at index, or null if there is none. Unlike window.localStorage, keys are sorted by character code, so storing or removing a key can change the index of others.
indexThe position of the key, starting from 0.
Example:
for (var i = 0; i < project.documentStorage.length; i++)
    log(project.documentStorage.key(i));

length

The number of stored keys.
Example:
log(project.documentStorage.length + ' keys are stored');

removeItem(key)

Removes key and its value. Does nothing if the key isn't stored.
keyThe key to remove.
Example:
project.documentStorage.removeItem('Q-data-cleaning-status-' + file.guid);

setItem(key, value)

Stores value under key, replacing any existing value. If this would exceed the 5 MB limit, it throws an error and doesn't store anything.
keyThe key to store the value under.
valueThe value to store. Use JSON.stringify() for anything other than a string.
Example:
project.documentStorage.setItem('Q-data-cleaning-status-' + file.guid, 'cleaned');

type

Returns the name we use to refer to this class in the documentation.