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