snappymail/dev/Model/AbstractCollection.js

42 lines
896 B
JavaScript
Raw Normal View History

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() {
this.forEach(item => item.onDestroy && item.onDestroy());
}
2020-10-19 01:36:57 +08:00
/**
* @static
* @param {FetchJson} json
* @returns {*CollectionModel}
*/
static reviveFromJson(json, itemCallback) {
2020-10-23 21:15:54 +08:00
if (json) {
const result = new this();
if ('Collection/'+this.name.replace('Model', '') === json['@Object']) {
Object.entries(json).forEach(([key, value]) => '@' !== key[0] && (result[key] = value));
// json[@Count]
json = json['@Collection'];
}
if (Array.isArray(json)) {
json.forEach(item => {
item && itemCallback && (item = itemCallback(item, result));
item && result.push(item);
});
return result;
}
2020-10-18 17:46:22 +08:00
}
2020-10-23 21:15:54 +08:00
return null;
2020-10-18 17:46:22 +08:00
}
}