From bde2a0187e8890aa4d1e47b3cb21c6645401fd05 Mon Sep 17 00:00:00 2001 From: Andrew Worcester Date: Wed, 2 Dec 2015 11:18:04 -0500 Subject: [PATCH 1/2] implemented _links property to hold link and relationship links while not breaking the _relationships array interface --- src/jsonapi-datastore.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/jsonapi-datastore.js b/src/jsonapi-datastore.js index e4e9696..c2d0409 100644 --- a/src/jsonapi-datastore.js +++ b/src/jsonapi-datastore.js @@ -165,6 +165,10 @@ class JsonApiDataStore { model[key] = rec.attributes[key]; } + if (rec.links) { + model._links = rec.links; + } + if (rec.relationships) { for (key in rec.relationships) { var rel = rec.relationships[key]; @@ -179,7 +183,8 @@ class JsonApiDataStore { } } if (rel.links) { - console.log("Warning: Links not implemented yet."); + model._links = model._links || {}; + model._links[key] = rel.links; } } } From 399bf69e18a982869df9c9e420cf246819c9c7aa Mon Sep 17 00:00:00 2001 From: Ben Morrall Date: Sat, 17 Jun 2017 14:08:09 +1000 Subject: [PATCH 2/2] Added relationships hash to links --- dist/jsonapi-datastore.js | 8 ++- dist/jsonapi-datastore.min.js | 2 +- dist/ng-jsonapi-datastore.js | 8 ++- dist/ng-jsonapi-datastore.min.js | 2 +- dist/node-jsonapi-datastore.js | 8 ++- dist/node-jsonapi-datastore.min.js | 2 +- src/jsonapi-datastore.js | 3 +- test/store.spec.js | 82 ++++++++++++++++++++++++++++++ 8 files changed, 108 insertions(+), 7 deletions(-) diff --git a/dist/jsonapi-datastore.js b/dist/jsonapi-datastore.js index 2ef8d3c..a4c98a7 100644 --- a/dist/jsonapi-datastore.js +++ b/dist/jsonapi-datastore.js @@ -201,6 +201,10 @@ var JsonApiDataStore = (function () { model[key] = rec.attributes[key]; } + if (rec.links) { + model._links = rec.links; + } + if (rec.relationships) { for (key in rec.relationships) { var rel = rec.relationships[key]; @@ -215,7 +219,9 @@ var JsonApiDataStore = (function () { } } if (rel.links) { - console.log("Warning: Links not implemented yet."); + model._links = model._links || {}; + model._links._relationships = model._links._relationships || {}; + model._links._relationships[key] = rel.links; } } } diff --git a/dist/jsonapi-datastore.min.js b/dist/jsonapi-datastore.min.js index c5ee19f..412dcfb 100644 --- a/dist/jsonapi-datastore.min.js +++ b/dist/jsonapi-datastore.min.js @@ -1 +1 @@ -"use strict";function _classCallCheck(t,i){if(!(t instanceof i))throw new TypeError("Cannot call a class as a function")}var _createClass=function(){function t(t,i){for(var e=0;e { expect(articles[1].related_article.id).to.eq(1337); }); }); + + context('when given a payload with a link', () => { + var store = new JsonApiDataStore(), + payload = { + data: { + type: 'article', + id: 1337, + links: { + self: 'https://example.com/articles/1337' + } + } + }; + + it('should set the links', () => { + var article = store.sync(payload); + expect(article._links.self).to.eq('https://example.com/articles/1337'); + }); + }); + + context('when given a payload with relationships containing links', () => { + var store = new JsonApiDataStore(), + payload = { + data: { + type: 'article', + id: 1337, + relationships: { + author: { + data: { + type: 'user', + id: 1 + }, + links: { + self: 'http://example.com/articles/1337/relationships/user', + related: 'http://example.com/articles/1337/user' + }, + } + } + } + }; + + it('should set the relationship links', () => { + var article = store.sync(payload); + expect(article._links._relationships.author.self).to.eq('http://example.com/articles/1337/relationships/user'); + expect(article._links._relationships.author.related).to.eq('http://example.com/articles/1337/user'); + }); + }); + + context('when given a payload with links and relationships containing links', () => { + var store = new JsonApiDataStore(), + payload = { + data: { + type: 'article', + id: 1337, + links: { + self: 'https://example.com/articles/1337' + }, + relationships: { + author: { + data: { + type: 'user', + id: 1 + }, + links: { + self: 'http://example.com/articles/1337/relationships/user', + related: 'http://example.com/articles/1337/user' + }, + } + } + } + }; + + it('should set the links', () => { + var article = store.sync(payload); + expect(article._links.self).to.eq('https://example.com/articles/1337'); + }); + + it('should set the relationship links', () => { + var article = store.sync(payload); + expect(article._links._relationships.author.self).to.eq('http://example.com/articles/1337/relationships/user'); + expect(article._links._relationships.author.related).to.eq('http://example.com/articles/1337/user'); + }); + }); }); describe('.syncWithMeta()', () => {