mirror of
https://github.com/the-djmaze/snappymail.git
synced 2026-07-08 22:18:28 +03:00
CollectionModels reviveFromJson
This commit is contained in:
parent
d747de7649
commit
7d89de2fdb
3 changed files with 69 additions and 71 deletions
|
|
@ -2,6 +2,11 @@
|
|||
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]
|
||||
|
|
@ -12,4 +17,21 @@ export class AbstractCollectionModel extends Array
|
|||
? object['@Collection']
|
||||
: null;
|
||||
}
|
||||
|
||||
static reviveFromJson(object, itemCallback) {
|
||||
// FolderCollectionModel => FolderCollection
|
||||
// AttachmentCollectionModel => AttachmentCollection
|
||||
// MessageCollectionModel => MessageCollection
|
||||
const name = this.name.replace('Model', ''),
|
||||
collection = this.getFromJSON(object, name);
|
||||
if (collection) {
|
||||
const result = new this(object);
|
||||
collection.forEach(item => {
|
||||
item && itemCallback && (item = itemCallback(item, result));
|
||||
item && result.push(item);
|
||||
});
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue