From 41fd7a8b721d4f6e80a488a9f830d093ae6b2f47 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Fri, 14 Aug 2020 13:31:43 -0400 Subject: [PATCH] REFACTOR: Add `themeSettings` to the `helperContext` to avoid a global --- .../javascripts/discourse-common/addon/lib/helpers.js | 6 ++---- .../javascripts/discourse/app/helpers/theme-helpers.js | 6 ++---- .../discourse/app/initializers/auto-load-modules.js | 3 ++- test/javascripts/test_helper.js | 2 +- 4 files changed, 7 insertions(+), 10 deletions(-) diff --git a/app/assets/javascripts/discourse-common/addon/lib/helpers.js b/app/assets/javascripts/discourse-common/addon/lib/helpers.js index 826a5855588..efdfdd53e8c 100644 --- a/app/assets/javascripts/discourse-common/addon/lib/helpers.js +++ b/app/assets/javascripts/discourse-common/addon/lib/helpers.js @@ -46,10 +46,8 @@ export function registerHelpers(registry) { } let _helperContext; -export function createHelperContext(siteSettings) { - _helperContext = { - siteSettings - }; +export function createHelperContext(ctx) { + _helperContext = ctx; } // This can be used by a helper to get the SiteSettings. Note you should not diff --git a/app/assets/javascripts/discourse/app/helpers/theme-helpers.js b/app/assets/javascripts/discourse/app/helpers/theme-helpers.js index 1e878656f5c..354d69d1d01 100644 --- a/app/assets/javascripts/discourse/app/helpers/theme-helpers.js +++ b/app/assets/javascripts/discourse/app/helpers/theme-helpers.js @@ -1,5 +1,5 @@ import I18n from "I18n"; -import { registerUnbound } from "discourse-common/lib/helpers"; +import { helperContext, registerUnbound } from "discourse-common/lib/helpers"; import deprecated from "discourse-common/lib/deprecated"; registerUnbound("theme-i18n", (themeId, key, params) => { @@ -18,7 +18,5 @@ registerUnbound("theme-setting", (themeId, key, hash) => { { since: "v2.2.0.beta8", dropFrom: "v2.3.0" } ); } - return Discourse.__container__ - .lookup("service:theme-settings") - .getSetting(themeId, key); + return helperContext().themeSettings.getSetting(themeId, key); }); diff --git a/app/assets/javascripts/discourse/app/initializers/auto-load-modules.js b/app/assets/javascripts/discourse/app/initializers/auto-load-modules.js index e68c919d979..b1b38738e76 100644 --- a/app/assets/javascripts/discourse/app/initializers/auto-load-modules.js +++ b/app/assets/javascripts/discourse/app/initializers/auto-load-modules.js @@ -16,7 +16,8 @@ export function autoLoadModules(container, registry) { } }); let siteSettings = container.lookup("site-settings:main"); - createHelperContext(siteSettings); + let themeSettings = container.lookup("service:theme-settings"); + createHelperContext({ siteSettings, themeSettings }); registerHelpers(registry); registerRawHelpers(RawHandlebars, Handlebars); } diff --git a/test/javascripts/test_helper.js b/test/javascripts/test_helper.js index 17e32185c93..95f2f6f84fd 100644 --- a/test/javascripts/test_helper.js +++ b/test/javascripts/test_helper.js @@ -164,7 +164,7 @@ QUnit.testStart(function(ctx) { Session.resetCurrent(); User.resetCurrent(); resetSite(settings); - createHelperContext(settings); + createHelperContext({ siteSettings: settings }); _DiscourseURL.redirectedTo = null; _DiscourseURL.redirectTo = function(url) {