2020-07-30 03:49:41 +08:00
|
|
|
|
|
|
|
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() {
|
2020-10-19 01:46:47 +08:00
|
|
|
/*
|
|
|
|
constructor(props) {
|
|
|
|
if (new.target === Parent) {
|
|
|
|
throw new Error("Can't instantiate abstract class!");
|
|
|
|
}
|
|
|
|
this.sModelName = new.target.name;
|
|
|
|
props && Object.entries(props).forEach(([key, value]) => '@' !== key[0] && (this[key] = value));
|
|
|
|
*/
|
2016-07-07 05:03:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
regDisposables(value) {
|
2020-07-30 03:49:41 +08:00
|
|
|
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() {
|
2020-07-30 03:49:41 +08:00
|
|
|
if (Array.isArray(this.disposables)) {
|
|
|
|
this.disposables.forEach(disposeOne);
|
|
|
|
}
|
|
|
|
Object.values(this).forEach(disposeOne);
|
2016-07-07 05:03:30 +08:00
|
|
|
}
|
2020-10-19 01:46:47 +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(json) : null;
|
|
|
|
}
|
|
|
|
|
2016-07-07 05:03:30 +08:00
|
|
|
}
|