Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/haydn/json-api-store. An isomorphic JavaScript library that acts as an in memory data store for JSON API data. particlesystem.com/json-api-store
json-api
Something went wrong. Try again.
5.4 kB · 217 lines
HTML
at master
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218<!DOCTYPE html><html><head> <meta charset="utf-8"> <base data-ice="baseUrl" href="../../"> <title data-ice="title">src/ajax-adapter.js | API Document</title> <link type="text/css" rel="stylesheet" href="css/style.css"> <link type="text/css" rel="stylesheet" href="css/prettify-tomorrow.css"> <script src="script/prettify/prettify.js"></script> </head><body class="layout-container">
<header> <a href="./">Home</a> <a href="identifiers.html">Identifier</a> <a href="source.html">Source</a> <a data-ice="repoURL" href="https://github.com/haydn/json-api-store" class="repo-url-github">Repository</a> <div class="search-box"> <span> <img src="./image/search.png"> <span class="search-input-edge"></span><input class="search-input"><span class="search-input-edge"></span> </span> <ul class="search-result"></ul> </div></header>
<nav class="navigation" data-ice="nav"><div data-ice="classWrap"> <h2>Class</h2> <ul> <li data-ice="classDoc"><span><a href="class/src/ajax-adapter.js~AjaxAdapter.html">AjaxAdapter</a></span></li><li data-ice="classDoc"><span><a href="class/src/store.js~Store.html">Store</a></span></li></ul></div>
<div data-ice="functionWrap"> <h2><a href="function/">Function</a></h2> <ul> <li data-ice="functionDoc"><span><a href="function/index.html#static-function-ajax">ajax</a></span></li></ul></div>
</nav>
<div class="content" data-ice="content"><h1 data-ice="title">src/ajax-adapter.js</h1><pre class="source-code line-number raw-source-code"><code class="prettyprint linenums" data-ice="content">import ajax from "./ajax";
export default class AjaxAdapter {
constructor(options) { this._base = (options && options.base) || ""; };
create(store, type, partial, options) {
if (!store._types[type]) { throw new Error(`Unknown type '${type}'`); }
let source = ajax({ body: JSON.stringify({ data: store.convert(type, partial) }), crossDomain: true, headers: { "Content-Type": "application/vnd.api+json" }, method: "POST", responseType: "auto", url: this._getUrl(type, null, options) }).do(e => store.push(e.response)) .map(e => store.find(e.response.data.type, e.response.data.id)) .publish();
source.connect();
return source;
}
destroy(store, type, id, options) {
if (!store._types[type]) { throw new Error(`Unknown type '${type}'`); }
let source = ajax({ crossDomain: true, headers: { "Content-Type": "application/vnd.api+json" }, method: "DELETE", responseType: "auto", url: this._getUrl(type, id, options) }).do(() => store.remove(type, id)) .publish();
source.connect();
return source;
}
load(store, type, id, options) {
if (id && typeof id === "object") { return this.load(store, type, null, id); }
if (!store._types[type]) { throw new Error(`Unknown type '${type}'`); }
let source = ajax({ crossDomain: true, headers: { "Content-Type": "application/vnd.api+json" }, method: "GET", responseType: "auto", url: this._getUrl(type, id, options) }).do(e => store.push(e.response)) .map(() => id ? store.find(type, id) : store.findAll(type)) .publish();
source.connect();
return source;
}
update(store, type, id, partial, options) {
if (!store._types[type]) { throw new Error(`Unknown type '${type}'`); }
let data = store.convert(type, id, partial);
let source = ajax({ body: JSON.stringify({ data: data }), crossDomain: true, headers: { "Content-Type": "application/vnd.api+json" }, method: "PATCH", responseType: "auto", url: this._getUrl(type, id, options) }).do(() => store.add(data)) .map(() => store.find(type, id)) .publish();
source.connect();
return source;
}
_getUrl(type, id, options) {
let params = []; let url = id ? `${this._base}/${type}/${id}` : `${this._base}/${type}`;
if (options) {
if (options.fields) { Object.keys(options.fields).forEach(field => { options[`fields[${field}]`] = options.fields[field]; }); delete options.fields; }
params = Object.keys(options).map(key => { return key + "=" + encodeURIComponent(options[key]); }).sort();
if (params.length) { url = `${url}?${params.join("&")}`; }
}
return url;
}
}</code></pre>
</div>
<footer class="footer"> Generated by <a href="https://esdoc.org">ESDoc<span data-ice="esdocVersion">(0.2.6)</span></a></footer>
<script src="script/search_index.js"></script><script src="script/search.js"></script><script src="script/pretty-print.js"></script><script src="script/inherited-summary.js"></script><script src="script/test-summary.js"></script><script src="script/inner-link.js"></script><script src="script/patch-for-local.js"></script></body></html>