snappymail/dev/Model/AbstractCollection.js

39 lines
875 B
JavaScript
Raw Normal View History

import { isArray, forEachObjectEntry } from 'Common/Utils';
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
}
*/
super();
}
onDestroy() {
2022-09-02 17:52:07 +08:00
this.forEach(item => item.onDestroy?.());
}
2020-10-19 01:36:57 +08:00
/**
* @static
* @param {FetchJson} json
* @returns {*CollectionModel}
*/
static reviveFromJson(json, itemCallback) {
const result = new this();
2020-10-23 21:15:54 +08:00
if (json) {
if ('Collection/'+this.name.replace('Model', '') === json['@Object']) {
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
}
return result;
2020-10-18 17:46:22 +08:00
}
}