From cc04e42444304df5bad395a6b6c27adc7fe07cc3 Mon Sep 17 00:00:00 2001 From: djmaze Date: Wed, 4 Nov 2020 23:57:37 +0100 Subject: [PATCH] Bugfix: revivePropertiesFromJson always set observables --- dev/Knoin/AbstractModel.js | 21 +++++++-------------- dev/Model/AbstractCollection.js | 5 ++--- 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/dev/Knoin/AbstractModel.js b/dev/Knoin/AbstractModel.js index d3f925a97..9b4b48e5d 100644 --- a/dev/Knoin/AbstractModel.js +++ b/dev/Knoin/AbstractModel.js @@ -13,10 +13,10 @@ function typeCast(curValue, newValue) { case 'string': return null != newValue ? '' + newValue : ''; case 'object': if (curValue.constructor.reviveFromJson) { - return curValue.constructor.reviveFromJson(newValue) || undefined; + return curValue.constructor.reviveFromJson(newValue); } - if (!Array.isArray(curValue) || !Array.isArray(newValue)) - return undefined; + if (Array.isArray(curValue) && !Array.isArray(newValue)) + return []; } return newValue; } @@ -95,12 +95,8 @@ export class AbstractModel { { case 'function': if (ko.isObservable(this[key])) { - value = typeCast(this[key](), value); - if (undefined !== value) { - this[key](value); - break; - } -// console.log((typeof this[key]())+' '+(model.name)+'.'+key+' not revived'); + this[key](typeCast(this[key](), value)); +// console.log('Observable ' + (typeof this[key]()) + ' ' + (model.name) + '.' + key + ' revived'); } // else console.log(model.name + '.' + key + ' is a function'); break; @@ -108,11 +104,8 @@ export class AbstractModel { case 'number': case 'object': case 'string': - value = typeCast(this[key], value); - if (undefined !== value) { - this[key] = value; - break; - } + this[key] = typeCast(this[key], value); + break; // fall through case 'undefined': default: diff --git a/dev/Model/AbstractCollection.js b/dev/Model/AbstractCollection.js index ae84b6334..c44425faa 100644 --- a/dev/Model/AbstractCollection.js +++ b/dev/Model/AbstractCollection.js @@ -20,8 +20,8 @@ export class AbstractCollectionModel extends Array * @returns {*CollectionModel} */ static reviveFromJson(json, itemCallback) { + const result = new this(); 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] @@ -32,10 +32,9 @@ export class AbstractCollectionModel extends Array item && itemCallback && (item = itemCallback(item, result)); item && result.push(item); }); - return result; } } - return null; + return result; } }