Skip to content

A platform for community discussion. Free, open, simple.

DEV: Reduce the `getOwnerWithFallback` usage (#34330)

Jarek Radosz07425a9feda5
diff --git a/app/assets/javascripts/discourse/app/routes/application.js b/app/assets/javascripts/discourse/app/routes/application.js
index 9fb41b72e06..e4f9cf20877 100644
--- a/app/assets/javascripts/discourse/app/routes/application.js
+++ b/app/assets/javascripts/discourse/app/routes/application.js
@@ -1,11 +1,11 @@
import { action } from "@ember/object";
+import { getOwner } from "@ember/owner";
import { service } from "@ember/service";
import KeyboardShortcutsHelp from "discourse/components/modal/keyboard-shortcuts-help";
import NotActivatedModal from "discourse/components/modal/not-activated";
import { RouteException } from "discourse/controllers/exception";
import { setting } from "discourse/lib/computed";
import deprecated from "discourse/lib/deprecated";
-import { getOwnerWithFallback } from "discourse/lib/get-owner";
import getURL from "discourse/lib/get-url";
import logout from "discourse/lib/logout";
import mobile from "discourse/lib/mobile";
@@ -176,16 +176,22 @@ export default class ApplicationRoute extends DiscourseRoute {
showLogin(props = {}) {
const t = this.router.transitionTo("login");
t.wantsTo = true;
- const controller = getOwnerWithFallback(this).lookup("controller:login");
- return t.then(() => controller.setProperties({ ...props }));
+ return t.then(() =>
+ getOwner(this)
+ .lookup("controller:login")
+ .setProperties({ ...props })
+ );
}
@action
showCreateAccount(props = {}) {
const t = this.router.transitionTo("signup");
t.wantsTo = true;
- const controller = getOwnerWithFallback(this).lookup("controller:signup");
- return t.then(() => controller.setProperties({ ...props }));
+ return t.then(() =>
+ getOwner(this)
+ .lookup("controller:signup")
+ .setProperties({ ...props })
+ );
}
@action
@@ -225,7 +231,7 @@ export default class ApplicationRoute extends DiscourseRoute {
"createNewTopicViaParam on the application route is deprecated. Use the composer service instead",
{ id: "discourse.createNewTopicViaParams" }
);
- getOwnerWithFallback(this).lookup("service:composer").openNewTopic({
+ this.composer.openNewTopic({
title,
body,
categoryId,
@@ -244,7 +250,7 @@ export default class ApplicationRoute extends DiscourseRoute {
"createNewMessageViaParams on the application route is deprecated. Use the composer service instead",
{ id: "discourse.createNewMessageViaParams" }
);
- getOwnerWithFallback(this).lookup("service:composer").openNewMessage({
+ this.composer.openNewMessage({
recipients,
title: topicTitle,
body: topicBody,
diff --git a/app/assets/javascripts/discourse/app/services/composer.js b/app/assets/javascripts/discourse/app/services/composer.js
index c9bdbc895eb..432e140991a 100644
--- a/app/assets/javascripts/discourse/app/services/composer.js
+++ b/app/assets/javascripts/discourse/app/services/composer.js
@@ -1,6 +1,7 @@
import { tracked } from "@glimmer/tracking";
import EmberObject, { action, computed } from "@ember/object";
import { alias, and, or, reads } from "@ember/object/computed";
+import { getOwner } from "@ember/owner";
import { cancel, next, scheduleOnce } from "@ember/runloop";
import Service, { service } from "@ember/service";
import { isEmpty } from "@ember/utils";
@@ -24,7 +25,6 @@ import prepareFormTemplateData, {
getFormTemplateObject,
} from "discourse/lib/form-template-validation";
import { shortDate } from "discourse/lib/formatter";
-import { getOwnerWithFallback } from "discourse/lib/get-owner";
import getURL from "discourse/lib/get-url";
import { disableImplicitInjections } from "discourse/lib/implicit-injections";
import { wantsNewWindow } from "discourse/lib/intercept-click";
@@ -137,7 +137,7 @@ export default class ComposerService extends Service {
@or("replyingToWhisper", "model.whisper") isWhispering;
get topicController() {
- return getOwnerWithFallback(this).lookup("controller:topic");
+ return getOwner(this).lookup("controller:topic");
}
get isPreviewVisible() {
@@ -275,10 +275,7 @@ export default class ComposerService extends Service {
@computed
get showToolbar() {
- const keyValueStore = getOwnerWithFallback(this).lookup(
- "service:key-value-store"
- );
- const storedVal = keyValueStore.get("toolbar-enabled");
+ const storedVal = this.keyValueStore.get("toolbar-enabled");
if (this._toolbarEnabled === undefined && storedVal === undefined) {
// iPhone 6 is 375, anything narrower and toolbar should
// be default disabled.
@@ -290,11 +287,8 @@ export default class ComposerService extends Service {
}
set showToolbar(val) {
- const keyValueStore = getOwnerWithFallback(this).lookup(
- "service:key-value-store"
- );
this._toolbarEnabled = val;
- keyValueStore.set({
+ this.keyValueStore.set({
key: "toolbar-enabled",
value: val ? "true" : "false",
});
diff --git a/app/assets/javascripts/discourse/tests/integration/components/decorated-html-test.gjs b/app/assets/javascripts/discourse/tests/integration/components/decorated-html-test.gjs
index 25046ec675c..d5e4011d464 100644
--- a/app/assets/javascripts/discourse/tests/integration/components/decorated-html-test.gjs
+++ b/app/assets/javascripts/discourse/tests/integration/components/decorated-html-test.gjs
@@ -1,4 +1,5 @@
import { tracked } from "@glimmer/tracking";
+import { getOwner } from "@ember/owner";
import { htmlSafe } from "@ember/template";
import { render, settled } from "@ember/test-helpers";
import { hbs } from "ember-cli-htmlbars";
@@ -6,7 +7,6 @@ import curryComponent from "ember-curry-component";
import { module, test } from "qunit";
import DecoratedHtml from "discourse/components/decorated-html";
import { withSilencedDeprecations } from "discourse/lib/deprecated";
-import { getOwnerWithFallback } from "discourse/lib/get-owner";
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
import { POST_STREAM_DEPRECATION_OPTIONS } from "discourse/widgets/widget";
@@ -68,7 +68,7 @@ module("Integration | Component | <DecoratedHtml />", function (hooks) {
<div id="render-glimmer">Hello from {{@value}} Component</div>
</template>,
{ value: "Curried" },
- getOwnerWithFallback(this)
+ getOwner(this)
)
);
};
diff --git a/plugins/chat/assets/javascripts/discourse/initializers/chat-setup.js b/plugins/chat/assets/javascripts/discourse/initializers/chat-setup.js
index d833228b500..8e5118c013d 100644
--- a/plugins/chat/assets/javascripts/discourse/initializers/chat-setup.js
+++ b/plugins/chat/assets/javascripts/discourse/initializers/chat-setup.js
@@ -3,7 +3,6 @@ import { service } from "@ember/service";
import EmojiPickerDetached from "discourse/components/emoji-picker/detached";
import { bind } from "discourse/lib/decorators";
import { number } from "discourse/lib/formatter";
-import { getOwnerWithFallback } from "discourse/lib/get-owner";
import { replaceIcon } from "discourse/lib/icon-library";
import { withPluginApi } from "discourse/lib/plugin-api";
import { i18n } from "discourse-i18n";
@@ -121,9 +120,7 @@ class ChatSetupInit {
// we want to decorate the chat quote dates regardless
// of whether the current user has chat enabled
api.decorateCookedElement((elem) => {
- const currentUser = getOwnerWithFallback(this).lookup(
- "service:current-user"
- );
+ const currentUser = owner.lookup("service:current-user");
const currentUserTimezone = currentUser?.user_option?.timezone;
const chatTranscriptElements =
elem.querySelectorAll(".chat-transcript");
diff --git a/plugins/discourse-templates/assets/javascripts/discourse/initializers/d-templates.js b/plugins/discourse-templates/assets/javascripts/discourse/initializers/d-templates.js
index a4b7135983c..631a226747a 100644
--- a/plugins/discourse-templates/assets/javascripts/discourse/initializers/d-templates.js
+++ b/plugins/discourse-templates/assets/javascripts/discourse/initializers/d-templates.js
@@ -1,4 +1,3 @@
-import { getOwnerWithFallback } from "discourse/lib/get-owner";
import { PLATFORM_KEY_MODIFIER } from "discourse/lib/keyboard-shortcuts";
import { withPluginApi } from "discourse/lib/plugin-api";
import extractVariablesFromChatChannel from "../../lib/variables-chat-channel";
@@ -84,10 +83,10 @@ function addChatIntegration(api, container) {
}
const channelVariablesExtractor = function () {
- const chat = getOwnerWithFallback(this).lookup("service:chat");
+ const chat = container.lookup("service:chat");
const activeChannel = chat?.activeChannel;
const currentMessage = activeChannel?.draft;
- const router = getOwnerWithFallback(this).lookup("service:router");
+ const router = container.lookup("service:router");
return extractVariablesFromChatChannel(
activeChannel,
@@ -97,10 +96,10 @@ function addChatIntegration(api, container) {
};
const threadVariablesExtractor = function () {
- const chat = getOwnerWithFallback(this).lookup("service:chat");
+ const chat = container.lookup("service:chat");
const activeThread = chat?.activeChannel?.activeThread;
const currentMessage = activeThread?.draft;
- const router = getOwnerWithFallback(this).lookup("service:router");
+ const router = container.lookup("service:router");
return extractVariablesFromChatThread(activeThread, currentMessage, router);
};
@@ -126,7 +125,7 @@ function addChatIntegration(api, container) {
const textarea = this.composer?.textarea?.textarea; // this.composer.textarea is a TextareaInteractor instance
- getOwnerWithFallback(this)
+ container
.lookup("service:d-templates")
.showTextAreaUI(contextVariablesExtractor, textarea);
},