From fc377fb6e1608c17728fc8aba3f0d5713cb5fc50 Mon Sep 17 00:00:00 2001 From: Daniele Ghidoli Date: Sun, 18 Sep 2016 13:23:43 +0200 Subject: [PATCH] Add rollbackAttributes() method to model --- src/models/json-api.model.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/models/json-api.model.ts b/src/models/json-api.model.ts index 936d56e9..b3edadfb 100644 --- a/src/models/json-api.model.ts +++ b/src/models/json-api.model.ts @@ -43,6 +43,25 @@ export class JsonApiModel { return hasDirtyAttributes; } + rollbackAttributes() { + let attributesMetadata = Reflect.getMetadata('Attribute', this); + let metadata: any; + for (let propertyName in attributesMetadata) { + if (attributesMetadata.hasOwnProperty(propertyName)) { + if (attributesMetadata[propertyName].hasDirtyAttributes) { + this[propertyName] = attributesMetadata[propertyName].oldValue; + metadata = { + hasDirtyAttributes: false, + newValue: attributesMetadata[propertyName].oldValue, + oldValue: undefined + }; + attributesMetadata[propertyName] = metadata; + } + } + } + Reflect.defineMetadata('Attribute', attributesMetadata, this); + } + private parseHasMany(data: any, included: any, level: number) { let hasMany = Reflect.getMetadata('HasMany', this); if (hasMany) {