snappymail/dev/Knoin/AbstractModel.js

65 lines
1.3 KiB
JavaScript
Raw Normal View History

function disposeOne(disposable) {
if (disposable && 'function' === typeof disposable.dispose) {
disposable.dispose();
}
}
2016-07-07 05:03:30 +08:00
2019-07-05 03:19:24 +08:00
export class AbstractModel {
2016-09-10 06:38:16 +08:00
disposables = [];
2016-07-07 05:03:30 +08:00
/**
2016-09-10 06:38:16 +08:00
* @param {string} modelName = ''
2016-07-07 05:03:30 +08:00
*/
2020-10-19 01:19:45 +08:00
constructor() {
/*
if (new.target === Parent) {
throw new Error("Can't instantiate abstract class!");
}
this.sModelName = new.target.name;
*/
2016-07-07 05:03:30 +08:00
}
regDisposables(value) {
if (Array.isArray(value)) {
2020-10-02 18:40:33 +08:00
value.forEach(item => this.disposables.push(item));
2019-07-05 03:19:24 +08:00
} else if (value) {
2016-07-07 05:03:30 +08:00
this.disposables.push(value);
}
}
2019-07-05 03:19:24 +08:00
onDestroy() {
if (Array.isArray(this.disposables)) {
this.disposables.forEach(disposeOne);
}
Object.values(this).forEach(disposeOne);
2016-07-07 05:03:30 +08:00
}
/**
* @static
* @param {FetchJson} json
* @returns {boolean}
*/
static validJson(json) {
return !!(json && ('Object/'+this.name.replace('Model', '') === json['@Object']));
}
/**
* @static
* @param {FetchJson} json
* @returns {*Model}
*/
static reviveFromJson(json) {
// Object/Attachment
// Object/Contact
// Object/Email
// Object/Filter
// Object/Folder
// Object/Message
// Object/Template
return this.validJson(json) ? new this() : null;
// json && Object.entries(json).forEach(([key, value]) => '@' !== key[0] && (this[key] = value));
}
2016-07-07 05:03:30 +08:00
}