DEV: Improve RestModel injections workaround (#23435)

We have a workaround so that currentUser/siteSettings/appEvents work properly on RestModel instances which are created without an owner. This is not ideal, but fixing this properly is not trivial. This commit improves the workaround to be more robust and support all service injections.
This commit is contained in:
David Taylor 2023-09-06 15:11:05 +01:00 committed by GitHub
parent 19567daeb9
commit f73d8346c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 16 deletions

View File

@ -1,7 +1,8 @@
import EmberObject from "@ember/object";
import { Promise } from "rsvp";
import { equal } from "@ember/object/computed";
import { getOwner } from "discourse-common/lib/get-owner";
import { getOwner as getOwnerWithFallback } from "discourse-common/lib/get-owner";
import { getOwner, setOwner } from "@ember/application";
import { warn } from "@ember/debug";
const RestModel = EmberObject.extend({
@ -101,23 +102,19 @@ RestModel.reopenClass({
create(args) {
args = args || {};
let owner = getOwner(this);
// Some Discourse code calls `model.create()` directly without going through the
// store. In that case the injections are not made, so we do them here. Eventually
// we should use the store for everything to fix this.
if (!args.store) {
args.store = owner.lookup("service:store");
}
if (!args.siteSettings) {
args.siteSettings = owner.lookup("service:site-settings");
}
if (!args.appEvents) {
args.appEvents = owner.lookup("service:app-events");
}
args.__munge = this.munge;
return this._super(this.munge(args, args.store));
const createArgs = this.munge(args, args.store);
// Some Discourse code calls `model.create()` directly without going through the
// store. In that case the owner is not set, and injections will fail. This workaround ensures
// the owner is always present. Eventually we should use the store for everything to fix this.
const receivedOwner = getOwner(createArgs);
if (!receivedOwner || receivedOwner.isDestroyed) {
setOwner(createArgs, getOwnerWithFallback());
}
return this._super(createArgs);
},
});