Merge pull request #3200 from flarum/as/1_2_assorted_frontend_patches

A few frontend fixes for v1.2
This commit is contained in:
Alexander Skvortsov 2021-12-13 16:31:18 -05:00 committed by GitHub
commit 3892d9b1fa

View File

@ -110,6 +110,7 @@ export default abstract class Model {
}
if ('attributes' in data) {
this.data.attributes ||= {};
Object.assign(this.data.attributes, data.attributes);
}
@ -295,16 +296,14 @@ export default abstract class Model {
static hasOne<M extends Model | null>(name: string): () => M | null | false;
static hasOne<M extends Model>(name: string): () => M | false {
return function (this: Model) {
if (this.data.relationships) {
const relationshipData = this.data.relationships[name]?.data;
const relationshipData = this.data.relationships?.[name]?.data;
if (relationshipData instanceof Array) {
throw new Error(`Relationship ${name} on model ${this.data.type} is plural, so the hasOne method cannot be used to access it.`);
}
if (relationshipData && relationshipData instanceof Array) {
throw new Error(`Relationship ${name} on model ${this.data.type} is plural, so the hasOne method cannot be used to access it.`);
}
if (relationshipData) {
return this.store.getById<M>(relationshipData.type, relationshipData.id) as M;
}
if (relationshipData) {
return this.store.getById<M>(relationshipData.type, relationshipData.id) as M;
}
return false;
@ -321,16 +320,14 @@ export default abstract class Model {
*/
static hasMany<M extends Model>(name: string): () => (M | undefined)[] | false {
return function (this: Model) {
if (this.data.relationships) {
const relationshipData = this.data.relationships[name]?.data;
const relationshipData = this.data.relationships?.[name]?.data;
if (!(relationshipData instanceof Array)) {
throw new Error(`Relationship ${name} on model ${this.data.type} is singular, so the hasMany method cannot be used to access it.`);
}
if (relationshipData && !(relationshipData instanceof Array)) {
throw new Error(`Relationship ${name} on model ${this.data.type} is singular, so the hasMany method cannot be used to access it.`);
}
if (relationshipData) {
return relationshipData.map((data) => this.store.getById<M>(data.type, data.id));
}
if (relationshipData) {
return relationshipData.map((data) => this.store.getById<M>(data.type, data.id));
}
return false;