snappymail/dev/Knoin/AbstractModel.js

124 lines
3 KiB
JavaScript
Raw Normal View History

2021-03-24 21:22:25 +08:00
import { isArray, isFunction, addObservablesTo, addComputablesTo } from 'Common/Utils';
function dispose(disposable) {
2021-03-24 21:22:25 +08:00
if (disposable && isFunction(disposable.dispose)) {
disposable.dispose();
}
}
2016-07-07 05:03:30 +08:00
2020-10-25 18:46:58 +08:00
function typeCast(curValue, newValue) {
switch (typeof curValue)
{
2021-03-13 06:54:47 +08:00
case 'boolean': return 0 != newValue && !!newValue;
2020-10-25 18:46:58 +08:00
case 'number': return isFinite(newValue) ? parseFloat(newValue) : 0;
case 'string': return null != newValue ? '' + newValue : '';
2020-10-23 21:15:54 +08:00
case 'object':
2020-10-25 18:46:58 +08:00
if (curValue.constructor.reviveFromJson) {
return curValue.constructor.reviveFromJson(newValue);
2020-10-23 21:15:54 +08:00
}
if (isArray(curValue) && !isArray(newValue))
return [];
}
2020-10-25 18:46:58 +08:00
return newValue;
}
2019-07-05 03:19:24 +08:00
export class AbstractModel {
2020-10-19 01:19:45 +08:00
constructor() {
/*
if (new.target === AbstractModel) {
2020-10-23 21:15:54 +08:00
throw new Error("Can't instantiate AbstractModel!");
}
*/
this.subscribables = [];
2016-07-07 05:03:30 +08:00
}
addObservables(observables) {
addObservablesTo(this, observables);
2020-10-25 18:46:58 +08:00
}
addComputables(computables) {
addComputablesTo(this, computables);
2020-10-25 21:14:14 +08:00
}
addSubscribables(subscribables) {
Object.entries(subscribables).forEach(([key, fn]) => this.subscribables.push( this[key].subscribe(fn) ) );
2016-07-07 05:03:30 +08:00
}
/** Called by delegateRunOnDestroy */
2019-07-05 03:19:24 +08:00
onDestroy() {
/** dispose ko subscribables */
this.subscribables.forEach(dispose);
/** clear object entries */
// Object.entries(this).forEach(([key, value]) => {
Object.values(this).forEach(value => {
/** clear CollectionModel */
let arr = ko.isObservableArray(value) ? value() : value;
arr && arr.onDestroy && value.onDestroy();
/** destroy ko.observable/ko.computed? */
dispose(value);
/** clear object value */
// this[key] = null; // TODO: issue with Contacts view
2020-10-25 18:46:58 +08:00
});
// this.subscribables = [];
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) {
let obj = this.validJson(json) ? new this() : null;
2020-10-23 21:15:54 +08:00
obj && obj.revivePropertiesFromJson(json);
return obj;
}
revivePropertiesFromJson(json) {
let model = this.constructor;
try {
2020-10-23 21:15:54 +08:00
if (!model.validJson(json)) {
return false;
}
Object.entries(json).forEach(([key, value]) => {
if ('@' !== key[0]) {
key = key[0].toLowerCase() + key.substr(1);
2020-10-23 21:15:54 +08:00
switch (typeof this[key])
{
case 'function':
2020-10-23 21:15:54 +08:00
if (ko.isObservable(this[key])) {
this[key](typeCast(this[key](), value));
// console.log('Observable ' + (typeof this[key]()) + ' ' + (model.name) + '.' + key + ' revived');
}
2020-10-23 21:15:54 +08:00
// else console.log(model.name + '.' + key + ' is a function');
break;
case 'boolean':
case 'number':
2020-10-23 21:15:54 +08:00
case 'object':
case 'string':
this[key] = typeCast(this[key], value);
break;
2020-10-23 21:15:54 +08:00
// fall through
case 'undefined':
default:
// console.log((typeof this[key])+' '+(model.name)+'.'+key+' not revived');
}
}
});
} catch (e) {
2020-10-23 21:15:54 +08:00
console.log(model.name);
console.error(e);
}
2020-10-23 21:15:54 +08:00
return true;
}
2016-07-07 05:03:30 +08:00
}