Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/models/json-api.model.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,28 @@ describe('JsonApiModel', () => {
});
});
});

describe('update relationships', () => {
it ('should return updated relationship', () => {
const REL = 'books';
const BOOK_NUMBER = 1;
const CHAPTERS_NUMBER = 4;
const DATA = getAuthorData(REL, BOOK_NUMBER);
const INCLUDED = getIncludedBooks(BOOK_NUMBER);
const NEW_BOOK_TITLE = 'The Hobbit'
author = new Author(datastore, DATA);
author.syncRelationships(DATA, INCLUDED, 0);
INCLUDED.forEach(model => {
if (model.type === 'books') {
model.attributes.title = NEW_BOOK_TITLE;
}
})
author.syncRelationships(DATA, INCLUDED, 0);
author.books.forEach(book => {
expect(book.title).toBe(NEW_BOOK_TITLE);
});
});
});
});
});
});
2 changes: 2 additions & 0 deletions src/models/json-api.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import find from 'lodash-es/find';
import { Observable } from 'rxjs/Observable';
import { JsonApiDatastore, ModelType } from '../services/json-api-datastore.service';
import { ModelConfig } from '../interfaces/model-config.interface';
import * as _ from 'lodash';

export class JsonApiModel {
id: string;
Expand Down Expand Up @@ -142,6 +143,7 @@ export class JsonApiModel {
private createOrPeek<T extends JsonApiModel>(modelType: ModelType<T>, data: any): T {
let peek = this._datastore.peekRecord(modelType, data.id);
if (peek) {
_.extend(peek, data.attributes);
return peek;
}
let newObject: T = new modelType(this._datastore, data);
Expand Down