ModelCookieDelegate
- 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.
- function ModelCookieDelegate() {
- }
- ModelCookieDelegate.prototype = Object.create(Objective.prototype);
- Object.defineProperty(ModelCookieDelegate.prototype, 'constructor', { value: ModelCookieDelegate, enumerable: false, writable: true });
An instance of ModelCookieDelegate inherits from the class Objective.
- ModelCookieDelegate.prototype.isSaved = function(model) {
- return Cookies.get(model.name) !== undefined;
- };
isSaved
returns true
if a cookie named after the name of model
exists, false
otherwise.
- ModelCookieDelegate.prototype.readIn = function(model) {
- let json = Cookies.get(model.name);
- if (json !== undefined)
- model.set(JSON.parse(json));
- };
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
.
- ModelCookieDelegate.prototype.writeOut = function(model) {
- Cookies.set(model.name, JSON.stringify(model.get()), { path: '/', sameSite: 'lax' });
- model.changed = false;
- };
writeOut
assigns the value in JSON of model
to a cookie named after the name of model
.
- ModelCookieDelegate.prototype.clearSave = function(model) {
- Cookies.remove(model.name, { path: '/' });
- };
clearSave
deletes the cookie named after the name of model
.
Comments