Skip to content

Commit

Permalink
REFACTOR: We can reuse getOwner for some container stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
eviltrout committed Aug 25, 2020
1 parent 347a498 commit a3fb732
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 2 additions & 8 deletions app/assets/javascripts/discourse/app/lib/plugin-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1273,5 +1268,4 @@ function decorate(klass, evt, cb, id) {

export function resetPluginApi() {
_pluginv01 = null;
_container = null;
}
14 changes: 6 additions & 8 deletions app/assets/javascripts/discourse/app/lib/show-modal.js
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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" };
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,15 @@ 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 {
name: "discourse-bootstrap",

// 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
Expand Down
6 changes: 1 addition & 5 deletions test/javascripts/helpers/qunit-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -211,7 +208,6 @@ export function acceptance(name, options) {
setTopicList(null);
_clearSnapshots();
setURLContainer(null);
setModalContainer(null);
setDefaultOwner(null);
Discourse._runInitializer(
"instanceInitializers",
Expand Down

0 comments on commit a3fb732

Please sign in to comment.