REFACTOR: Remove container from raw template helper

This commit is contained in:
Robin Ward 2020-08-19 11:15:56 -04:00
parent e08545cf99
commit 20e1233f89
2 changed files with 20 additions and 24 deletions

View File

@ -1,36 +1,23 @@
import { registerUnbound } from "discourse-common/lib/helpers"; import { registerUnbound, helperContext } from "discourse-common/lib/helpers";
import { findRawTemplate } from "discourse-common/lib/raw-templates"; import { findRawTemplate } from "discourse-common/lib/raw-templates";
import { htmlSafe } from "@ember/template"; import { htmlSafe } from "@ember/template";
import { setOwner } from "@ember/application";
let _injections; function renderRaw(ctx, template, templateName, params) {
function renderRaw(ctx, container, template, templateName, params) {
params = jQuery.extend({}, params); params = jQuery.extend({}, params);
params.parent = params.parent || ctx; params.parent = params.parent || ctx;
if (!_injections) { let context = helperContext();
_injections = {
siteSettings: container.lookup("site-settings:main"),
currentUser: container.lookup("current-user:main"),
site: container.lookup("site:main"),
session: container.lookup("session:main"),
topicTrackingState: container.lookup("topic-tracking-state:main")
};
setOwner(_injections, container);
}
if (!params.view) { if (!params.view) {
const module = `discourse/raw-views/${templateName}`; const module = `discourse/raw-views/${templateName}`;
if (requirejs.entries[module]) { if (requirejs.entries[module]) {
const viewClass = requirejs(module, null, null, true); const viewClass = requirejs(module, null, null, true);
if (viewClass && viewClass.default) { if (viewClass && viewClass.default) {
params.view = viewClass.default.create(params, _injections); params.view = viewClass.default.create(params, context);
} }
} }
if (!params.view) { if (!params.view) {
params = jQuery.extend({}, params, _injections); params = jQuery.extend({}, params, context);
} }
} }
@ -40,12 +27,11 @@ function renderRaw(ctx, container, template, templateName, params) {
registerUnbound("raw", function(templateName, params) { registerUnbound("raw", function(templateName, params) {
templateName = templateName.replace(".", "/"); templateName = templateName.replace(".", "/");
const container = Discourse.__container__;
const template = findRawTemplate(templateName); const template = findRawTemplate(templateName);
if (!template) { if (!template) {
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.warn("Could not find raw template: " + templateName); console.warn("Could not find raw template: " + templateName);
return; return;
} }
return renderRaw(this, container, template, templateName, params); return renderRaw(this, template, templateName, params);
}); });

View File

@ -5,6 +5,7 @@ import {
import RawHandlebars from "discourse-common/lib/raw-handlebars"; import RawHandlebars from "discourse-common/lib/raw-handlebars";
import { registerRawHelpers } from "discourse-common/lib/raw-handlebars-helpers"; import { registerRawHelpers } from "discourse-common/lib/raw-handlebars-helpers";
import Handlebars from "handlebars"; import Handlebars from "handlebars";
import { setOwner } from "@ember/application";
export function autoLoadModules(container, registry) { export function autoLoadModules(container, registry) {
Object.keys(requirejs.entries).forEach(entry => { Object.keys(requirejs.entries).forEach(entry => {
@ -15,10 +16,19 @@ export function autoLoadModules(container, registry) {
requirejs(entry, null, null, true); requirejs(entry, null, null, true);
} }
}); });
let siteSettings = container.lookup("site-settings:main");
let themeSettings = container.lookup("service:theme-settings"); let context = {
let keyValueStore = container.lookup("key-value-store:main"); siteSettings: container.lookup("site-settings:main"),
createHelperContext({ siteSettings, themeSettings, keyValueStore }); themeSettings: container.lookup("service:theme-settings"),
keyValueStore: container.lookup("key-value-store:main"),
currentUser: container.lookup("current-user:main"),
site: container.lookup("site:main"),
session: container.lookup("session:main"),
topicTrackingState: container.lookup("topic-tracking-state:main")
};
setOwner(context, container);
createHelperContext(context);
registerHelpers(registry); registerHelpers(registry);
registerRawHelpers(RawHandlebars, Handlebars); registerRawHelpers(RawHandlebars, Handlebars);
} }