resourcerer 2.1.0 → 2.1.1
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/build/lib/collection.d.ts +2 -1
- package/build/lib/collection.js +7 -4
- package/build/lib/model.js +1 -1
- package/package.json +1 -1
- package/test/events.test.js +32 -0
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import Events from "./events.js";
|
|
2
|
-
import Model, { type ConstructorOptions, type SetOptions } from "./model.js";
|
|
2
|
+
import Model, { type CanonicalModelSubscription, type ConstructorOptions, type SetOptions } from "./model.js";
|
|
3
3
|
import sync, { type SyncOptions } from "./sync.js";
|
|
4
4
|
import { ResourceConfigObj } from "./types.js";
|
|
5
5
|
type CSetOptions = {
|
|
@@ -26,6 +26,7 @@ export default class Collection<T extends Record<string, any> = object, O extend
|
|
|
26
26
|
urlOptions: Record<string, any>;
|
|
27
27
|
static Model: new (...args: any[]) => Model;
|
|
28
28
|
static idAttribute: string;
|
|
29
|
+
static subscriptions: CanonicalModelSubscription[];
|
|
29
30
|
static dependencies: Array<string | ((attrs: Record<string, any>) => Record<string, any>)>;
|
|
30
31
|
static cacheTimeout: number;
|
|
31
32
|
static comparator: comparator;
|
package/build/lib/collection.js
CHANGED
|
@@ -26,6 +26,7 @@ export default class Collection extends Events {
|
|
|
26
26
|
urlOptions = {};
|
|
27
27
|
static Model = Model;
|
|
28
28
|
static idAttribute;
|
|
29
|
+
static subscriptions = [];
|
|
29
30
|
static dependencies = [];
|
|
30
31
|
static cacheTimeout;
|
|
31
32
|
static comparator;
|
|
@@ -184,13 +185,15 @@ export default class Collection extends Events {
|
|
|
184
185
|
this.models.forEach((model) => model.unsubscribe());
|
|
185
186
|
}
|
|
186
187
|
_getModelClass() {
|
|
187
|
-
|
|
188
|
-
|
|
188
|
+
const { idAttribute, subscriptions } = this.constructor;
|
|
189
|
+
const DefaultModel = this.constructor.Model;
|
|
190
|
+
if (idAttribute || subscriptions.length) {
|
|
189
191
|
return class extends Model {
|
|
190
|
-
static idAttribute =
|
|
192
|
+
static idAttribute = idAttribute || DefaultModel.idAttribute;
|
|
193
|
+
static subscriptions = subscriptions;
|
|
191
194
|
};
|
|
192
195
|
}
|
|
193
|
-
return
|
|
196
|
+
return DefaultModel;
|
|
194
197
|
}
|
|
195
198
|
_reset() {
|
|
196
199
|
this.length = 0;
|
package/build/lib/model.js
CHANGED
|
@@ -155,8 +155,8 @@ export default class Model extends Events {
|
|
|
155
155
|
if (options.wait && !this.isNew()) {
|
|
156
156
|
this.triggerUpdate();
|
|
157
157
|
this.collection?.remove(this, { silent: true });
|
|
158
|
-
this.unsubscribe();
|
|
159
158
|
}
|
|
159
|
+
this.unsubscribe();
|
|
160
160
|
return [this, response];
|
|
161
161
|
})
|
|
162
162
|
.catch((response) => {
|
package/package.json
CHANGED
package/test/events.test.js
CHANGED
|
@@ -220,6 +220,19 @@ describe("Events", () => {
|
|
|
220
220
|
expect(collection.get("1234").get("name")).toEqual("C. Zorah");
|
|
221
221
|
});
|
|
222
222
|
|
|
223
|
+
it("when the targetModel is part of a collection with shorthand subscriptions defined", () => {
|
|
224
|
+
const collection = new SubscribingTargetShorthandCollection([{ id: "1234" }]);
|
|
225
|
+
|
|
226
|
+
sourceModel.set({ _id: "1234", name: "Zorah" });
|
|
227
|
+
expect(collection.get("1234").get("name")).toEqual("C. Zorah");
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
it("when the source model is part of a collection with shorthand subscriptions defined", () => {
|
|
231
|
+
new SubscribingSourceShorthandCollection([{ _id: "1234", name: "Zorah" }]);
|
|
232
|
+
|
|
233
|
+
expect(targetModel.get("name")).toEqual("C. Zorah");
|
|
234
|
+
});
|
|
235
|
+
|
|
223
236
|
it("when the target is also a source", () => {
|
|
224
237
|
const sourceModel = new SubscribingSourceAndTargetModel({ id: "1234" });
|
|
225
238
|
const targetModel = new SubscribingSourceAndTargetModel({ id: "1234" });
|
|
@@ -273,6 +286,25 @@ class SubscribingTargetCollection extends Collection {
|
|
|
273
286
|
static Model = SubscribingTargetModel;
|
|
274
287
|
}
|
|
275
288
|
|
|
289
|
+
class SubscribingTargetShorthandCollection extends Collection {
|
|
290
|
+
static subscriptions = [
|
|
291
|
+
{
|
|
292
|
+
Model: CanonicalTestModel,
|
|
293
|
+
fromSource: (attrs) => ({ id: attrs._id, name: "C. " + attrs.name }),
|
|
294
|
+
},
|
|
295
|
+
];
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
class SubscribingSourceShorthandCollection extends Collection {
|
|
299
|
+
static subscriptions = [
|
|
300
|
+
{
|
|
301
|
+
Model: CanonicalTestModel,
|
|
302
|
+
idField: "_id",
|
|
303
|
+
toSource: (attrs) => ({ _id: attrs._id, name: attrs.name }),
|
|
304
|
+
},
|
|
305
|
+
];
|
|
306
|
+
}
|
|
307
|
+
|
|
276
308
|
class SubscribingSourceAndTargetModel extends Model {
|
|
277
309
|
static subscriptions = [
|
|
278
310
|
{
|