A platform for community discussion. Free, open, simple.
DEV: Add `owner` arg to plugin connector's `shouldRender` callbacks (#29488)
Jarek Radoszdc96b6e95399
diff --git a/app/assets/javascripts/discourse/app/components/plugin-outlet.js b/app/assets/javascripts/discourse/app/components/plugin-outlet.jsindex a35ca161023..aa50a47a5b1 100644--- a/app/assets/javascripts/discourse/app/components/plugin-outlet.js+++ b/app/assets/javascripts/discourse/app/components/plugin-outlet.js@@ -1,6 +1,7 @@import { cached } from "@glimmer/tracking";import ClassicComponent from "@ember/component";import { get } from "@ember/object";+import { getOwner } from "@ember/owner";import { service } from "@ember/service";import GlimmerComponentWithDeprecatedParentView from "discourse/components/glimmer-component-with-deprecated-parent-view";import {@@ -88,7 +89,8 @@ export default class PluginOutletComponent extends GlimmerComponentWithDeprecateconst connectors = renderedConnectorsFor(this.args.name,this.outletArgsWithDeprecations,- this.context+ this.context,+ getOwner(this));if (connectors.length > 1 && hasBlock) {const message = `Multiple connectors were registered for the ${this.args.name} outlet. Using the first.`;diff --git a/app/assets/javascripts/discourse/app/lib/plugin-connectors.js b/app/assets/javascripts/discourse/app/lib/plugin-connectors.jsindex 8e33c5c1cdf..dcd0f7be676 100644--- a/app/assets/javascripts/discourse/app/lib/plugin-connectors.js+++ b/app/assets/javascripts/discourse/app/lib/plugin-connectors.js@@ -224,10 +224,12 @@ export function connectorsFor(outletName) {return _connectorCache[outletName] || [];}-export function renderedConnectorsFor(outletName, args, context) {+export function renderedConnectorsFor(outletName, args, context, owner) {return connectorsFor(outletName).filter((con) => {- const shouldRender = con.connectorClass?.shouldRender;- return !shouldRender || shouldRender(args, context);+ return (+ !con.connectorClass?.shouldRender ||+ con.connectorClass?.shouldRender(args, context, owner)+ );});}diff --git a/app/assets/javascripts/discourse/tests/integration/components/plugin-outlet-test.gjs b/app/assets/javascripts/discourse/tests/integration/components/plugin-outlet-test.gjsindex cd6a55ffc49..009945fb681 100644--- a/app/assets/javascripts/discourse/tests/integration/components/plugin-outlet-test.gjs+++ b/app/assets/javascripts/discourse/tests/integration/components/plugin-outlet-test.gjs@@ -60,8 +60,12 @@ module("Integration | Component | plugin-outlet", function (hooks) {});registerTemporaryModule(`${CLASS_PREFIX}/test-name/conditional-render`, {- shouldRender(args, context) {- return args.shouldDisplay || context.siteSettings.always_display;+ shouldRender(args, context, owner) {+ return (+ args.shouldDisplay ||+ context.siteSettings.always_display ||+ owner.lookup("service:site-settings").alternativeAccess+ );},});@@ -407,6 +411,17 @@ module("Integration | Component | plugin-outlet", function (hooks) {assert.dom(".conditional-render").exists("renders conditional outlet");});+ test("shouldRender receives an owner argument", async function (assert) {+ await render(hbs`<PluginOutlet @name="test-name" />`);+ assert+ .dom(".conditional-render")+ .doesNotExist("doesn't render conditional outlet");++ getOwner(this).lookup("service:site-settings").alternativeAccess = true;+ await settled();+ assert.dom(".conditional-render").exists("renders conditional outlet");+ });+test("Other outlets are not re-rendered", async function (assert) {this.set("shouldDisplay", false);await render(hbs`