From a3fb732b2311772e8f58629d69227702f7236743 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Wed, 19 Aug 2020 15:47:49 -0400 Subject: [PATCH] REFACTOR: We can reuse `getOwner` for some container stuff --- .../discourse-common/addon/lib/get-owner.js | 2 +- .../javascripts/discourse/app/lib/plugin-api.js | 10 ++-------- .../javascripts/discourse/app/lib/show-modal.js | 14 ++++++-------- .../app/pre-initializers/discourse-bootstrap.js | 4 ---- test/javascripts/helpers/qunit-helpers.js | 6 +----- 5 files changed, 10 insertions(+), 26 deletions(-) diff --git a/app/assets/javascripts/discourse-common/addon/lib/get-owner.js b/app/assets/javascripts/discourse-common/addon/lib/get-owner.js index 23a7218fa60..f8831b15edc 100644 --- a/app/assets/javascripts/discourse-common/addon/lib/get-owner.js +++ b/app/assets/javascripts/discourse-common/addon/lib/get-owner.js @@ -5,7 +5,7 @@ let _default = {}; export function getOwner(obj) { if (emberGetOwner) { - return emberGetOwner(obj) || emberGetOwner(_default); + return emberGetOwner(obj || _default) || emberGetOwner(_default); } return obj.container; diff --git a/app/assets/javascripts/discourse/app/lib/plugin-api.js b/app/assets/javascripts/discourse/app/lib/plugin-api.js index b49eb573f4e..6e38f22f075 100644 --- a/app/assets/javascripts/discourse/app/lib/plugin-api.js +++ b/app/assets/javascripts/discourse/app/lib/plugin-api.js @@ -59,6 +59,7 @@ import { on } from "@ember/object/evented"; import { addQuickAccessProfileItem } from "discourse/widgets/quick-access-profile"; import KeyboardShortcuts from "discourse/lib/keyboard-shortcuts"; import { addFeaturedLinkMetaDecorator } from "discourse/lib/render-topic-featured-link"; +import { getOwner } from "discourse-common/lib/get-owner"; // If you add any methods to the API ensure you bump up this number const PLUGIN_API_VERSION = "0.10.2"; @@ -1200,17 +1201,11 @@ function cmpVersions(a, b) { return segmentsA.length - segmentsB.length; } -let _container; - -export function setPluginContainer(container) { - _container = container; -} - function getPluginApi(version) { version = version.toString(); if (cmpVersions(version, PLUGIN_API_VERSION) <= 0) { if (!_pluginv01) { - _pluginv01 = new PluginApi(version, _container); + _pluginv01 = new PluginApi(version, getOwner(this)); } // We are recycling the compatible object, but let's update to the higher version @@ -1273,5 +1268,4 @@ function decorate(klass, evt, cb, id) { export function resetPluginApi() { _pluginv01 = null; - _container = null; } diff --git a/app/assets/javascripts/discourse/app/lib/show-modal.js b/app/assets/javascripts/discourse/app/lib/show-modal.js index bc409f0f8f2..d76583b6deb 100644 --- a/app/assets/javascripts/discourse/app/lib/show-modal.js +++ b/app/assets/javascripts/discourse/app/lib/show-modal.js @@ -1,17 +1,15 @@ import I18n from "I18n"; import { dasherize } from "@ember/string"; - -let _container; -export function setModalContainer(container) { - _container = container; -} +import { getOwner } from "discourse-common/lib/get-owner"; export default function(name, opts) { opts = opts || {}; + let container = getOwner(this); + // We use the container here because modals are like singletons // in Discourse. Only one can be shown with a particular state. - const route = _container.lookup("route:application"); + const route = container.lookup("route:application"); const modalController = route.controllerFor("modal"); modalController.set( @@ -22,7 +20,7 @@ export default function(name, opts) { const controllerName = opts.admin ? `modals/${name}` : name; modalController.set("name", controllerName); - let controller = _container.lookup("controller:" + controllerName); + let controller = container.lookup("controller:" + controllerName); const templateName = opts.templateName || dasherize(name); const renderArgs = { into: "modal", outlet: "modalBody" }; @@ -31,7 +29,7 @@ export default function(name, opts) { } else { // use a basic controller renderArgs.controller = "basic-modal-body"; - controller = _container.lookup(`controller:${renderArgs.controller}`); + controller = container.lookup(`controller:${renderArgs.controller}`); } if (opts.addModalBodyView) { diff --git a/app/assets/javascripts/discourse/app/pre-initializers/discourse-bootstrap.js b/app/assets/javascripts/discourse/app/pre-initializers/discourse-bootstrap.js index d8772f3bd17..20c10a81e8d 100644 --- a/app/assets/javascripts/discourse/app/pre-initializers/discourse-bootstrap.js +++ b/app/assets/javascripts/discourse/app/pre-initializers/discourse-bootstrap.js @@ -11,9 +11,7 @@ import { import { setupURL, setupS3CDN } from "discourse-common/lib/get-url"; import deprecated from "discourse-common/lib/deprecated"; import { setIconList } from "discourse-common/lib/icon-library"; -import { setPluginContainer } from "discourse/lib/plugin-api"; import { setURLContainer } from "discourse/lib/url"; -import { setModalContainer } from "discourse/lib/show-modal"; import { setDefaultOwner } from "discourse-common/lib/get-owner"; export default { @@ -21,9 +19,7 @@ export default { // The very first initializer to run initialize(container, app) { - setPluginContainer(container); setURLContainer(container); - setModalContainer(container); setDefaultOwner(container); // Our test environment has its own bootstrap code diff --git a/test/javascripts/helpers/qunit-helpers.js b/test/javascripts/helpers/qunit-helpers.js index 931d09de1f3..7d1018beb38 100644 --- a/test/javascripts/helpers/qunit-helpers.js +++ b/test/javascripts/helpers/qunit-helpers.js @@ -5,7 +5,7 @@ import { later } from "@ember/runloop"; import sessionFixtures from "fixtures/session-fixtures"; import HeaderComponent from "discourse/components/site-header"; import { forceMobile, resetMobile } from "discourse/lib/mobile"; -import { resetPluginApi, setPluginContainer } from "discourse/lib/plugin-api"; +import { resetPluginApi } from "discourse/lib/plugin-api"; import { clearCache as clearOutletCache, resetExtraClasses @@ -29,7 +29,6 @@ import { currentSettings, mergeSettings } from "helpers/site-settings"; import { getOwner } from "discourse-common/lib/get-owner"; import { setTopicList } from "discourse/lib/topic-list-tracker"; import { setURLContainer } from "discourse/lib/url"; -import { setModalContainer } from "discourse/lib/show-modal"; import { setDefaultOwner } from "discourse-common/lib/get-owner"; export function currentUser() { @@ -173,9 +172,7 @@ export function acceptance(name, options) { Discourse.reset(); this.container = getOwner(this); - setPluginContainer(this.container); setURLContainer(this.container); - setModalContainer(this.container); setDefaultOwner(this.container); if (options.site) { @@ -211,7 +208,6 @@ export function acceptance(name, options) { setTopicList(null); _clearSnapshots(); setURLContainer(null); - setModalContainer(null); setDefaultOwner(null); Discourse._runInitializer( "instanceInitializers",