ModelStorageDelegate
- ModelStorageDelegate
An instance of ModelStorageDelegate lets an instance of Model read and write data in JSON in the storage space of the navigator. As a delegate, all the methods of an instance of ModelStorageDelegate are always called through an instance of Model.
- function ModelStorageDelegate(mode = 'session') {
- if (! (mode == 'session' || mode == 'local'))
- throw new TypeError();
- this._storage = mode == 'local' ? localStorage : sessionStorage;
- }
- ModelStorageDelegate.prototype = Object.create(Objective.prototype);
- Object.defineProperty(ModelStorageDelegate.prototype, 'constructor', { value: ModelStorageDelegate, enumerable: false, writable: true });
An instance of ModelStorageDelegate inherits from the class Objective.
mode
indicates whether the data is stored in a separate session or locally and more permanently by the navigator, i.d. 'session'
or 'local'
. The mode 'session'
is taken by default.
See the article Web Storage API on the website MDN Web Docs.
- ModelStorageDelegate.prototype.isSaved = function(model) {
- return this._storage.getItem(model.name) !== null;
- };
isSaved
returns true
if the storage space of this
has an item named after the name of model
, false
otherwise.
- ModelStorageDelegate.prototype.readIn = function(model) {
- let json = this._storage.getItem(model.name);
- if (json !== null)
- model.set(JSON.parse(json));
- };
readIn
recovers the content in JSON of the item in the storage space of this
named after the name of model
and if defined, decodes it and assigns it to model
.
- ModelStorageDelegate.prototype.writeOut = function(model) {
- this._storage.setItem(model.name, JSON.stringify(model.get()));
- model.changed = false;
- };
writeOut
assigns the value in JSON of model
to an item in the storage space of this
named after the name of model
.
- ModelStorageDelegate.prototype.clearSave = function(model) {
- this._storage.removeItem(model.name);
- };
clearSave
deletes the item in the storage space of this
named after the name of model
.
Comments