2021-12-01 21:23:37 +08:00
|
|
|
import { isArray, forEachObjectEntry } from 'Common/Utils';
|
2020-09-16 22:33:53 +08:00
|
|
|
|
|
|
|
export class AbstractCollectionModel extends Array
|
|
|
|
{
|
2020-10-23 21:15:54 +08:00
|
|
|
constructor() {
|
2020-10-18 17:46:22 +08:00
|
|
|
/*
|
2020-10-23 21:15:54 +08:00
|
|
|
if (new.target === AbstractCollectionModel) {
|
|
|
|
throw new Error("Can't instantiate AbstractCollectionModel!");
|
2020-10-18 17:46:22 +08:00
|
|
|
}
|
|
|
|
*/
|
2020-09-16 22:33:53 +08:00
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
2020-10-26 19:54:03 +08:00
|
|
|
onDestroy() {
|
2022-09-02 17:52:07 +08:00
|
|
|
this.forEach(item => item.onDestroy?.());
|
2020-10-26 19:54:03 +08:00
|
|
|
}
|
|
|
|
|
2020-10-19 01:36:57 +08:00
|
|
|
/**
|
|
|
|
* @static
|
|
|
|
* @param {FetchJson} json
|
|
|
|
* @returns {*CollectionModel}
|
|
|
|
*/
|
|
|
|
static reviveFromJson(json, itemCallback) {
|
2020-11-05 06:57:37 +08:00
|
|
|
const result = new this();
|
2020-10-23 21:15:54 +08:00
|
|
|
if (json) {
|
|
|
|
if ('Collection/'+this.name.replace('Model', '') === json['@Object']) {
|
2021-12-01 21:23:37 +08:00
|
|
|
forEachObjectEntry(json, (key, value) => '@' !== key[0] && (result[key] = value));
|
2020-10-23 21:15:54 +08:00
|
|
|
json = json['@Collection'];
|
|
|
|
}
|
2022-10-10 19:52:56 +08:00
|
|
|
isArray(json) && json.forEach(item => {
|
|
|
|
item && itemCallback && (item = itemCallback(item, result));
|
|
|
|
item && result.push(item);
|
|
|
|
});
|
2020-10-18 17:46:22 +08:00
|
|
|
}
|
2020-11-05 06:57:37 +08:00
|
|
|
return result;
|
2020-10-18 17:46:22 +08:00
|
|
|
}
|
|
|
|
|
2020-09-16 22:33:53 +08:00
|
|
|
}
|