Skip to content

Commit

Permalink
Fix detecting of dirty attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
ghidoz committed Sep 17, 2016
1 parent e1876d8 commit a38fa1c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
13 changes: 7 additions & 6 deletions src/decorators/attribute.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export function Attribute(config: any = {}) {

let saveAnnotations = function (hasDirtyAttributes: boolean, oldValue: any, newValue: any) {
let annotations = Reflect.getMetadata('Attribute', target) || {};
hasDirtyAttributes = typeof oldValue === 'undefined' ? false : hasDirtyAttributes;
annotations[propertyName] = {
hasDirtyAttributes: hasDirtyAttributes,
oldValue: oldValue,
Expand All @@ -13,19 +14,19 @@ export function Attribute(config: any = {}) {
Reflect.defineMetadata('Attribute', annotations, target);
};

let _val = target[propertyName];
saveAnnotations(false, undefined, _val);

let getter = function () {
return _val;
return this['_' + propertyName];
};

let setter = function (newVal: any) {
saveAnnotations(true, _val, newVal);
_val = newVal;
if (newVal !== this['_' + propertyName]) {
saveAnnotations(true, this['_' + propertyName], newVal);
this['_' + propertyName] = newVal;
}
};

if (delete target[propertyName]) {
saveAnnotations(false, undefined, target[propertyName]);
Object.defineProperty(target, propertyName, {
get: getter,
set: setter,
Expand Down
15 changes: 15 additions & 0 deletions src/models/json-api.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ export class JsonApiModel {
return this._datastore.saveRecord(attributesMetadata, this, params, headers);
}

hasDirtyAttributes() {
let attributesMetadata = Reflect.getMetadata('Attribute', this);
let hasDirtyAttributes = false;
for (let propertyName in attributesMetadata) {
if (attributesMetadata.hasOwnProperty(propertyName)) {
let metadata: any = attributesMetadata[propertyName];
if (metadata.hasDirtyAttributes) {
hasDirtyAttributes = true;
break;
}
}
}
return hasDirtyAttributes;
}

private parseHasMany(data: any, included: any, level: number) {
let hasMany = Reflect.getMetadata('HasMany', this);
if (hasMany) {
Expand Down
9 changes: 5 additions & 4 deletions src/services/json-api-datastore.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class JsonApiDatastore {
httpCall = this.http.post(url, body, options);
}
return httpCall
.map((res: any) => this.extractRecordData(res, modelType))
.map((res: any) => this.extractRecordData(res, modelType, data))
.map((res: any) => this.resetMetadataAttributes(res, attributesMetadata, modelType))
.catch((res: any) => this.handleError(res));
}
Expand Down Expand Up @@ -127,9 +127,9 @@ export class JsonApiDatastore {
return models;
}

private extractRecordData(res: any, modelType: ModelType): JsonApiModel {
private extractRecordData(res: any, modelType: ModelType, data?: any): JsonApiModel {
let body = res.json();
let model: JsonApiModel = new modelType(this, body.data);
let model: JsonApiModel = data || new modelType(this, body.data);
if (body.included) {
model.syncRelationships(body.data, body.included, 0);
}
Expand Down Expand Up @@ -201,6 +201,7 @@ export class JsonApiDatastore {
}

private resetMetadataAttributes(res: any, attributesMetadata: any, modelType: ModelType) {
attributesMetadata = Reflect.getMetadata('Attribute', res);
for (let propertyName in attributesMetadata) {
if (attributesMetadata.hasOwnProperty(propertyName)) {
let metadata: any = attributesMetadata[propertyName];
Expand All @@ -209,7 +210,7 @@ export class JsonApiDatastore {
}
}
}
Reflect.defineMetadata('Attribute', attributesMetadata, modelType);
Reflect.defineMetadata('Attribute', attributesMetadata, res);
return res;
}

Expand Down

0 comments on commit a38fa1c

Please sign in to comment.