Motherbrain w/ JSONAPI Client

by Chad Drummond

JavaScript

// https://github.com/beauby/jsonapi-datastore
"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<i.length;e++){var a=i[e];a.enumerable=a.enumerable||!1,a.configurable=!0,"value"in a&&(a.writable=!0),Object.defineProperty(t,a.key,a)}}return function(i,e,a){return e&&t(i.prototype,e),a&&t(i,a),i}}(),JsonApiDataStoreModel=function(){function t(i,e){_classCallCheck(this,t),this.id=e,this._type=i,this._attributes=[],this._relationships=[]}return _createClass(t,[{key:"serialize",value:function(t){var i=this,e={data:{type:this._type}};return t=t||{},t.attributes=t.attributes||this._attributes,t.relationships=t.relationships||this._relationships,void 0!==this.id&&(e.data.id=this.id),0!==t.attributes.length&&(e.data.attributes={}),0!==t.relationships.length&&(e.data.relationships={}),t.attributes.forEach(function(t){e.data.attributes[t]=i[t]}),t.relationships.forEach(function(t){function a(t){return{type:t._type,id:t.id}}i[t]?i[t].constructor===Array?e.data.relationships[t]={data:i[t].map(a)}:e.data.relationships[t]={data:a(i[t])}:e.data.relationships[t]={data:null}}),e}},{key:"setAttribute",value:function(t,i){void 0===this[t]&&this._attributes.push(t),this[t]=i}},{key:"setRelationship",value:function(t,i){void 0===this[t]&&this._relationships.push(t),this[t]=i}}]),t}(),JsonApiDataStore=function(){function t(){_classCallCheck(this,t),this.graph={}}return _createClass(t,[{key:"destroy",value:function(t){delete this.graph[t._type][t.id]}},{key:"find",value:function(t,i){return this.graph[t]&&this.graph[t][i]?this.graph[t][i]:null}},{key:"findAll",value:function(t){var i=this;return this.graph[t]?Object.keys(i.graph[t]).map(function(e){return i.graph[t][e]}):[]}},{key:"reset",value:function(){this.graph={}}},{key:"initModel",value:function(t,i){return this.graph[t]=this.graph[t]||{},this.graph[t][i]=this.graph[t][i]||new...