mirror of
https://github.com/the-djmaze/snappymail.git
synced 2026-07-11 00:14:50 +03:00
Cleaner CollectionModel code
This commit is contained in:
parent
ddc866735c
commit
76648f04ae
2 changed files with 19 additions and 20 deletions
|
|
@ -12,21 +12,19 @@ export class AbstractCollectionModel extends Array
|
|||
// props[@Count]
|
||||
}
|
||||
|
||||
static getFromJSON(object, name) {
|
||||
return object && 'Collection/'+name === object['@Object'] && Array.isArray(object['@Collection'])
|
||||
? object['@Collection']
|
||||
: null;
|
||||
}
|
||||
|
||||
static reviveFromJson(object, itemCallback) {
|
||||
/**
|
||||
* @static
|
||||
* @param {FetchJson} json
|
||||
* @returns {*CollectionModel}
|
||||
*/
|
||||
static reviveFromJson(json, 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 => {
|
||||
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);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -10,14 +10,15 @@ export class AttachmentCollectionModel extends AbstractCollectionModel
|
|||
* @returns {AttachmentCollectionModel}
|
||||
*/
|
||||
static reviveFromJson(items) {
|
||||
let result = new AttachmentCollectionModel;
|
||||
items = this.getFromJSON(items, 'AttachmentCollection') || items;
|
||||
Array.isArray(items) && items.forEach(attachment => {
|
||||
attachment = AttachmentModel.newInstanceFromJson(attachment);
|
||||
if (attachment) {
|
||||
result.push(attachment);
|
||||
}
|
||||
});
|
||||
let cb = attachment => AttachmentModel.newInstanceFromJson(attachment),
|
||||
result = super.reviveFromJson(items, cb);
|
||||
if (!result) {
|
||||
result = new AttachmentCollectionModel;
|
||||
Array.isArray(items) && items.forEach(attachment => {
|
||||
attachment = cb(attachment);
|
||||
attachment && result.push(attachment);
|
||||
});
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue