snappymail/dev/Model/AbstractCollection.js
2020-10-18 19:36:57 +02:00

35 lines
920 B
JavaScript

export class AbstractCollectionModel extends Array
{
constructor(props) {
/*
if (new.target === Parent) {
throw new Error("Can't instantiate abstract class!");
}
*/
super();
props && Object.entries(props).forEach(([key, value]) => '@' !== key[0] && (this[key] = value));
// props[@Count]
}
/**
* @static
* @param {FetchJson} json
* @returns {*CollectionModel}
*/
static reviveFromJson(json, itemCallback) {
// FolderCollectionModel => FolderCollection
// AttachmentCollectionModel => AttachmentCollection
// MessageCollectionModel => MessageCollection
if (json && 'Collection/'+this.name.replace('Model', '') === json['@Object']
&& Array.isArray(json['@Collection'])) {
const result = new this(json);
json['@Collection'].forEach(item => {
item && itemCallback && (item = itemCallback(item, result));
item && result.push(item);
});
return result;
}
}
}