mirror of
https://github.com/the-djmaze/snappymail.git
synced 2024-11-14 11:44:54 +08:00
41 lines
896 B
JavaScript
41 lines
896 B
JavaScript
|
|
export class AbstractCollectionModel extends Array
|
|
{
|
|
constructor() {
|
|
/*
|
|
if (new.target === AbstractCollectionModel) {
|
|
throw new Error("Can't instantiate AbstractCollectionModel!");
|
|
}
|
|
*/
|
|
super();
|
|
}
|
|
|
|
onDestroy() {
|
|
this.forEach(item => item.onDestroy && item.onDestroy());
|
|
}
|
|
|
|
/**
|
|
* @static
|
|
* @param {FetchJson} json
|
|
* @returns {*CollectionModel}
|
|
*/
|
|
static reviveFromJson(json, itemCallback) {
|
|
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;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
}
|