2020-09-16 22:33:53 +08:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
*/
|
2020-09-16 22:33:53 +08:00
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
2020-10-26 19:54:03 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-09-16 22:33:53 +08:00
|
|
|
}
|