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