1

ModelCookieDelegate

Objective
  • ModelCookieDelegate

An instance of ModelCookieDelegate lets an instance of Model read and write data in JSON in a cookie. As a delegate, all the methods of an instance of ModelCookieDelegate are always called through an instance of Model.

The cookie used by an instance of ModelCookieDelegate calls the functions of the module js-cookie. Download it and install it in a folder of the web site, e.g. /js/js.cookie.js.

Add a tag such as <script src="/js/js.cookie.js"></script> to the <head> section of the document in HTML.

  1. function ModelCookieDelegate() {
  2. }
  3.  
  4. ModelCookieDelegate.prototype = Object.create(Objective.prototype);
  5.  
  6. Object.defineProperty(ModelCookieDelegate.prototype, 'constructor', { value: ModelCookieDelegate, enumerable: false, writable: true });

An instance of ModelCookieDelegate inherits from the class Objective.

  1. ModelCookieDelegate.prototype.isSaved = function(model) {
  2.     return Cookies.get(model.name) !== undefined;
  3. }

isSaved returns true if a cookie named after the name of model exists, false otherwise.

  1. ModelCookieDelegate.prototype.readIn = function(model) {
  2.     let json = Cookies.get(model.name);
  3.  
  4.     if (json !== undefined)
  5.         model.set(JSON.parse(json));
  6. }

readIn recovers the content in JSON of the cookie named after the name of model and if defined, decodes it and assigns it to model.

  1. ModelCookieDelegate.prototype.writeOut = function(model) {
  2.     Cookies.set(model.name, JSON.stringify(model.get()), { path: '/', sameSite: 'lax' });
  3.  
  4.     model.changed = false;
  5. }

writeOut assigns the value in JSON of model to a cookie named after the name of model.

  1. ModelCookieDelegate.prototype.clearSave = function(model) {
  2.     Cookies.remove(model.name, { path: '/' });
  3. }

clearSave deletes the cookie named after the name of model.

SEE ALSO

ModelStorageDelegate, 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].