8000 Fix #15 - Remove mandatory store option · Wikodit/js-data-jsonapi-light@d7a36e6 · GitHub
[go: up one dir, main page]

Skip to content

Commit d7a36e6

Browse files
committed
Fix #15 - Remove mandatory store option
1 parent 7eed842 commit d7a36e6

File tree

7 files changed

+46
-63
lines changed

7 files changed

+46
-63
lines changed

README.md

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,8 @@ const store = new DataStore();
4848

4949
// Initialize our Adapter
5050
const jsonApiAdapter = new JsonApiAdapter({
51-
// Store needs to be given to the adapter
52-
store: store
53-
5451
// Same options as DSHttpAdapter
55-
// If a serialization option is given, it will be run before JSONApi serialization has occured
56-
// If a deserialization option is given, it will be run after JSONApi deserialization has occured
52+
// However, you should use before/after deserialize/serialize instead of serialize/deserialize
5753
});
5854

5955
// Register the Adapter as the default one in the store
@@ -120,7 +116,6 @@ Sometime you want to be able to do some custom additional serialization / deseri
120116

121117
```js
122118
const jsonApiAdapter = new JsonApiAdapter({
123-
store: store,
124119
beforeDeserialize: function(mapper, data, opts) { return data; }
125120
afterDeserialize: function(mapper, data, opts) { return data; }
126121
beforeSerialize: function(mapper, data, opts) { return data; }

dist/js-data-jsonapi-light.js

Lines changed: 39 additions & 42 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/js-data-jsonapi-light.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/adapter.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,19 @@ import { jsonApiSerialize, wrapSerialize } from './serializer';
66
import { ERROR } from './strings';
77

88
export class JsonApiAdapter extends HttpAdapter{
9-
private store: DataStore;
109
private options: any;
1110

1211
constructor(options?:any) {
1312
options = utils.deepMixIn({
1413
// Some default
1514
}, options || {})
1615

17-
if (!options.store) {
18-
throw new Error(ERROR.FORCE_STORE_OPTION)
19-
}
20-
2116
if (options.serialize || options.deserialize) {
2217
throw new Error(ERROR.PREVENT_SERIALIZE_DESERIALIZE_OPTIONS)
2318
}
2419

2520
super(options);
2621

27-
this.store = options.store;
2822
this.options = options;
2923

3024
this.serialize = wrapSerialize(this);

src/deserializer.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ export function wrapDeserialize(self:any):any{
1818
export function jsonApiDeserialize(mapper:Mapper, res:any, opts:any){
1919
if (!res.data || !res.data.data) return;
2020

21+
const store = (<any>mapper).datastore;
22+
2123
const collectionReceived = utils.isArray(res.data.data)
2224

2325
// We store all possible items stores in the response in a Object[type][id]
@@ -43,7 +45,7 @@ export function jsonApiDeserialize(mapper:Mapper, res:any, opts:any){
4345
// Know we will check every possible relationships and try to affect them
4446
// the correct key/id
4547
for (let type in itemsIndexed) {
46-
let resource:any = this.store.getMapper(type);
48+
let resource:any = store.getMapper(type);
4749
if (!resource) { this.warn(WARNING.NO_RESSOURCE(type)); continue; }
4850

4951
// Just cache a pointer to relations for the Resource

test/ds.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ export const store = window.store
1313

1414
const jsonApiAdapter = new JSDataJsonApiLight.JsonApiAdapter({
1515
suffix: '.json',
16-
basePath: 'api',
17-
store: store
16+
basePath: 'api'
1817
});
1918

2019
store.registerAdapter('jsonApi', jsonApiAdapter, { default: true })

test/unit/configuration.spec.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,5 @@ declare var JSData:any;
55
declare var JSDataJsonApiLight:any;
66

77
describe('Configuration :', () => {
8-
it('should throw an error if store is not given as adapter property', () => {
9-
assert.throws(() => {
10-
new JSDataJsonApiLight.JsonApiAdapter()
11-
}, Error, 'JsonApiAdapter needs to be given a store option.')
12-
})
8+
139
});

0 commit comments

Comments
 (0)
0