Skip to content

Commit

Permalink
allow ManyArray to work (for now)
Browse files Browse the repository at this point in the history
  • Loading branch information
runspired committed Sep 15, 2022
1 parent 06f3fc3 commit 1229195
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
8 changes: 6 additions & 2 deletions packages/model/addon/-private/legacy-relationships-support.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { A, default as EmberArray } from '@ember/array';
import { assert } from '@ember/debug';
import { DEBUG } from '@glimmer/env';

Expand Down Expand Up @@ -619,13 +620,16 @@ function handleCompletedRelationshipRequest(
}

function assertRecordsPassedToHasMany(records: RecordInstance[]) {
assert(`You must pass an array of records to set a hasMany relationship`, Array.isArray(records));
assert(
`You must pass an array of records to set a hasMany relationship`,
Array.isArray(records) || EmberArray.detect(records)
);
assert(
`All elements of a hasMany relationship must be instances of Model, you passed ${records
.map((r) => `${typeof r}`)
.join(', ')}`,
(function () {
return records.every((record) => Object.prototype.hasOwnProperty.call(record, '_internalModel') === true);
return A(records).every((record) => Object.prototype.hasOwnProperty.call(record, '_internalModel') === true);
})()
);
}
Expand Down
9 changes: 7 additions & 2 deletions packages/store/addon/-private/instance-cache.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { A, default as EmberArray } from '@ember/array';
import { assert } from '@ember/debug';
import { DEBUG } from '@glimmer/env';

Expand Down Expand Up @@ -349,13 +350,17 @@ export class InstanceCache {
}

function assertRecordsPassedToHasMany(records: RecordInstance[]) {
assert(`You must pass an array of records to set a hasMany relationship`, Array.isArray(records));
assert(
`You must pass an array of records to set a hasMany relationship`,
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
Array.isArray(records) || EmberArray.detect(records)
);
assert(
`All elements of a hasMany relationship must be instances of Model, you passed ${records
.map((r) => `${typeof r}`)
.join(', ')}`,
(function () {
return records.every((record) => Object.prototype.hasOwnProperty.call(record, '_internalModel') === true);
return A(records).every((record) => Object.prototype.hasOwnProperty.call(record, '_internalModel') === true);
})()
);
}
Expand Down

0 comments on commit 1229195

Please sign in to comment.