resourcerer 1.0.7 → 1.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/collection.js CHANGED
@@ -471,7 +471,7 @@ export default class Collection {
471
471
  return attrs;
472
472
  }
473
473
 
474
- return new this.Model(attrs, {...options, collection: this});
474
+ return new this.Model(attrs, {...this.urlOptions, ...options, collection: this});
475
475
  }
476
476
 
477
477
  /**
package/lib/model.js CHANGED
@@ -3,6 +3,16 @@ import {isDeepEqual, result, uniqueId, urlError} from './utils';
3
3
  import Events from './events';
4
4
  import sync from './sync';
5
5
 
6
+ const RESERVED_OPTION_KEYS = [
7
+ 'Model',
8
+ 'comparator',
9
+ 'parse',
10
+ 'collection',
11
+ 'silent',
12
+ 'method',
13
+ 'unset'
14
+ ];
15
+
6
16
  /**
7
17
  * The Model class should be extended for any resource that is singular, as in not a list of items.
8
18
  * It is the core class used to represent server data in the client application (Collections are
@@ -26,8 +36,6 @@ export default class Model {
26
36
  * * ...any other options that .set() takes
27
37
  */
28
38
  constructor(attributes={}, options={}) {
29
- const RESERVED_OPTION_KEYS = ['parse', 'collection', 'silent', 'unset'];
30
-
31
39
  this.cid = uniqueId('c');
32
40
  this.attributes = {};
33
41
 
@@ -204,9 +204,7 @@ export const useResources = (getResources, props) => {
204
204
  // https://stackoverflow.com/questions/48563650/
205
205
  // does-react-keep-the-order-for-state-updates/48610973#48610973
206
206
  ReactDOM.unstable_batchedUpdates(() => {
207
- if (resources.filter(withoutPrefetch).length) {
208
- setModels(modelAggregator(resources.filter(withoutPrefetch)));
209
- }
207
+ setModels(modelAggregator([[name, config]]));
210
208
 
211
209
  loaderDispatch({
212
210
  type: LoadingStates.LOADED,
@@ -214,10 +212,17 @@ export const useResources = (getResources, props) => {
214
212
  });
215
213
  });
216
214
  },
217
- onRequestFailure: (status, [name, config]) => loaderDispatch({
218
- type: LoadingStates.ERROR,
219
- payload: {name, config, status}
220
- })
215
+ onRequestFailure: (status, [name, config]) => {
216
+ // request failed, which means this model should not be in the cache. but we still want to
217
+ // set our model state so that when we go back into a loading state, the empty model
218
+ // is present instead of any previously-loaded model
219
+ setModels(modelAggregator([[name, config]]));
220
+
221
+ loaderDispatch({
222
+ type: LoadingStates.ERROR,
223
+ payload: {name, config, status}
224
+ });
225
+ }
221
226
  }).then(() => {
222
227
  if (isMountedRef.current) {
223
228
  // only get those from the cache. models in state might can empty models, but even so i
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "resourcerer",
3
- "version": "1.0.7",
3
+ "version": "1.0.10",
4
4
  "repository": "github.com/noahgrant/resourcerer",
5
5
  "description": "Declarative data-fetching and caching framework for React",
6
6
  "keywords": [
@@ -50,6 +50,19 @@ describe('Collection', () => {
50
50
  expect(collection.toJSON()).toEqual([]);
51
51
  });
52
52
 
53
+ it('urlOptions get passed down to models', async() => {
54
+ collection = new Collection([{id: '1'}], {one: 1, two: 2, comparator: 'foo', parse: true});
55
+
56
+ expect(collection.get('1').urlOptions).toEqual({one: 1, two: 2});
57
+
58
+ collection.add({id: '2'});
59
+ expect(collection.get('2').urlOptions).toEqual({one: 1, two: 2});
60
+
61
+ sync.default.mockResolvedValue([[{id: '3'}], {}]);
62
+ await collection.fetch();
63
+ expect(collection.get('3').urlOptions).toEqual({one: 1, two: 2});
64
+ });
65
+
53
66
  it('adds any models passed to its models list', () => {
54
67
  var model1 = new Model,
55
68
  model2 = new Model;