23

ModelStorageDelegate

Objective
  • 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.

  1. function ModelStorageDelegate(mode = 'session') {
  2.     if (! (mode == 'session' || mode == 'local'))
  3.         throw new TypeError();
  4.  
  5.     this._storage = mode == 'local' ? localStorage : sessionStorage;
  6. }
  7.  
  8. ModelStorageDelegate.prototype = Object.create(Objective.prototype);
  9.  
  10. 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.

  1. ModelStorageDelegate.prototype.isSaved = function(model) {
  2.     return this._storage.getItem(model.name) !== null;
  3. };

isSaved returns true if the storage space of this has an item named after the name of model, false otherwise.

  1. ModelStorageDelegate.prototype.readIn = function(model) {
  2.     let json = this._storage.getItem(model.name);
  3.  
  4.     if (json !== null)
  5.         model.set(JSON.parse(json));
  6. };

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.

  1. ModelStorageDelegate.prototype.writeOut = function(model) {
  2.     this._storage.setItem(model.name, JSON.stringify(model.get()));
  3.  
  4.     model.changed = false;
  5. };

writeOut assigns the value in JSON of model to an item in the storage space of this named after the name of model.

  1. ModelStorageDelegate.prototype.clearSave = function(model) {
  2.     this._storage.removeItem(model.name);
  3. };

clearSave deletes the item in the storage space of this named after the name of model.

SEE ALSO

ModelCookieDelegate, Model, Editor

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].