resourcerer 1.0.0-beta-4 → 1.0.0-beta-5
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/README.md +1 -1
- package/lib/collection.js +29 -12
- package/package.json +1 -1
- package/test/collection.test.js +25 -7
package/README.md
CHANGED
|
@@ -127,7 +127,7 @@ There's a lot there, so let's unpack that a bit. There's also a lot more that we
|
|
|
127
127
|
1. [Nomenclature](#nomenclature)
|
|
128
128
|
1. [Tutorial](#tutorial)
|
|
129
129
|
1. [Intro](#tutorial)
|
|
130
|
-
1. [Other Props Returned from the Hook/Passed from the HOC (Loading States)](other-props-returned-from-the-hookpassed-from-the-hoc-loading-states)
|
|
130
|
+
1. [Other Props Returned from the Hook/Passed from the HOC (Loading States)](#other-props-returned-from-the-hookpassed-from-the-hoc-loading-states)
|
|
131
131
|
1. [Requesting Prop-driven Data](#requesting-prop-driven-data)
|
|
132
132
|
1. [Changing Props](#changing-props)
|
|
133
133
|
1. [Common Resource Config Options](#common-resource-config-options)
|
package/lib/collection.js
CHANGED
|
@@ -30,7 +30,7 @@ export default class Collection {
|
|
|
30
30
|
constructor(models, options={}) {
|
|
31
31
|
const RESERVED_OPTION_KEYS = ['Model', 'comparator', 'silent', 'parse'];
|
|
32
32
|
|
|
33
|
-
this.Model = options.Model || this.
|
|
33
|
+
this.Model = options.Model || this._getModelClass();
|
|
34
34
|
this.comparator = options.comparator || this.constructor.comparator;
|
|
35
35
|
|
|
36
36
|
this._reset();
|
|
@@ -53,6 +53,13 @@ export default class Collection {
|
|
|
53
53
|
*/
|
|
54
54
|
static Model = Model
|
|
55
55
|
|
|
56
|
+
/**
|
|
57
|
+
* Defines the property by which we can uniquely identify models in the collection. Override this
|
|
58
|
+
* if you're not defining your own custom Model class but still need to index by a different field
|
|
59
|
+
* than the default ('id').
|
|
60
|
+
*/
|
|
61
|
+
static modelIdAttribute = null
|
|
62
|
+
|
|
56
63
|
/**
|
|
57
64
|
* This is a list of keys (could be attribute keys, but also keys passed in from the options
|
|
58
65
|
* object when instantiated) that resourcerer should take into account when determining how to
|
|
@@ -218,7 +225,7 @@ export default class Collection {
|
|
|
218
225
|
}
|
|
219
226
|
|
|
220
227
|
return this._byId[obj] ||
|
|
221
|
-
this._byId[
|
|
228
|
+
this._byId[(this._isModel(obj) ? obj.attributes : obj)[this.Model.idAttribute]] ||
|
|
222
229
|
obj.cid && this._byId[obj.cid];
|
|
223
230
|
}
|
|
224
231
|
|
|
@@ -417,14 +424,24 @@ export default class Collection {
|
|
|
417
424
|
}
|
|
418
425
|
|
|
419
426
|
/**
|
|
420
|
-
*
|
|
421
|
-
*
|
|
427
|
+
* This function gets called when the collection is instantiated, and its return value is set to
|
|
428
|
+
* its Model property (and used to instantiate all its models). It's only used if a `Model`
|
|
429
|
+
* option isn't passed to the constructor, and by default it returns the Model base class. But
|
|
430
|
+
* if the collection has a `modelIdAttribute` static property, it will create a new Model class
|
|
431
|
+
* with the corresponding `idAttribute` property and return that.
|
|
422
432
|
*
|
|
423
|
-
* @
|
|
424
|
-
* @return {string} that model's unique identifier
|
|
433
|
+
* @return {Model} model class associated with the collection
|
|
425
434
|
*/
|
|
426
|
-
|
|
427
|
-
|
|
435
|
+
_getModelClass() {
|
|
436
|
+
if (this.constructor.modelIdAttribute) {
|
|
437
|
+
let attr = this.constructor.modelIdAttribute;
|
|
438
|
+
|
|
439
|
+
return class extends Model {
|
|
440
|
+
static idAttribute = attr
|
|
441
|
+
};
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
return this.constructor.Model;
|
|
428
445
|
}
|
|
429
446
|
|
|
430
447
|
/**
|
|
@@ -482,8 +499,8 @@ export default class Collection {
|
|
|
482
499
|
|
|
483
500
|
delete this._byId[model.cid];
|
|
484
501
|
|
|
485
|
-
if (this.
|
|
486
|
-
delete this._byId[this.
|
|
502
|
+
if (model.attributes[this.Model.idAttribute]) {
|
|
503
|
+
delete this._byId[model.attributes[this.Model.idAttribute]];
|
|
487
504
|
}
|
|
488
505
|
|
|
489
506
|
removed.push(model);
|
|
@@ -514,7 +531,7 @@ export default class Collection {
|
|
|
514
531
|
_addReference(model) {
|
|
515
532
|
this._byId[model.cid] = model;
|
|
516
533
|
|
|
517
|
-
let id = this.
|
|
534
|
+
let id = model.attributes[this.Model.idAttribute];
|
|
518
535
|
|
|
519
536
|
if (id || typeof id === 'number') {
|
|
520
537
|
this._byId[id] = model;
|
|
@@ -533,7 +550,7 @@ export default class Collection {
|
|
|
533
550
|
_removeReference(model) {
|
|
534
551
|
delete this._byId[model.cid];
|
|
535
552
|
|
|
536
|
-
let id = this.
|
|
553
|
+
let id = model.attributes[this.Model.idAttribute];
|
|
537
554
|
|
|
538
555
|
if (id) {
|
|
539
556
|
delete this._byId[id];
|
package/package.json
CHANGED
package/test/collection.test.js
CHANGED
|
@@ -617,21 +617,39 @@ describe('Collection', () => {
|
|
|
617
617
|
});
|
|
618
618
|
});
|
|
619
619
|
|
|
620
|
-
describe('
|
|
621
|
-
it('
|
|
620
|
+
describe('setting a model id attribute', () => {
|
|
621
|
+
it('on the model sets the id attributes for all models', () => {
|
|
622
622
|
class _Model extends Model {
|
|
623
623
|
static idAttribute = 'name'
|
|
624
624
|
}
|
|
625
625
|
|
|
626
|
+
// default is just id
|
|
627
|
+
collection = new Collection();
|
|
628
|
+
collection.add({id: 'noahgrant', name: 'Noah Grant'});
|
|
629
|
+
expect(collection.get('noahgrant')).toBeDefined();
|
|
630
|
+
expect(collection.get('Noah Grant')).not.toBeDefined();
|
|
631
|
+
expect(collection.get('noahgrant').id).toEqual('noahgrant');
|
|
632
|
+
expect(collection.Model.idAttribute).toEqual('id');
|
|
633
|
+
|
|
634
|
+
collection = new Collection([], {Model: _Model});
|
|
635
|
+
collection.add({id: 'noahgrant', name: 'Noah Grant'});
|
|
636
|
+
expect(collection.get('noahgrant')).not.toBeDefined();
|
|
637
|
+
expect(collection.get('Noah Grant')).toBeDefined();
|
|
638
|
+
expect(collection.get('Noah Grant').id).toEqual('Noah Grant');
|
|
639
|
+
expect(collection.Model.idAttribute).toEqual('name');
|
|
640
|
+
});
|
|
641
|
+
|
|
642
|
+
it('on the collection sets the id attribute for its models', () => {
|
|
626
643
|
class _Collection extends Collection {
|
|
627
|
-
static
|
|
644
|
+
static modelIdAttribute = 'name'
|
|
628
645
|
}
|
|
629
646
|
|
|
630
|
-
collection = new Collection();
|
|
631
|
-
expect(collection.modelId({id: 'noahgrant', name: 'Noah Grant'})).toEqual('noahgrant');
|
|
632
|
-
|
|
633
647
|
collection = new _Collection();
|
|
634
|
-
|
|
648
|
+
collection.add({id: 'noahgrant', name: 'Noah Grant'});
|
|
649
|
+
expect(collection.get('noahgrant')).not.toBeDefined();
|
|
650
|
+
expect(collection.get('Noah Grant')).toBeDefined();
|
|
651
|
+
expect(collection.get('Noah Grant').id).toEqual('Noah Grant');
|
|
652
|
+
expect(collection.Model.idAttribute).toEqual('name');
|
|
635
653
|
});
|
|
636
654
|
});
|
|
637
655
|
|