Rerender on new route or transition

I’m building a banner ad plug-in. I have a section of the DOM that I need to re-render each time the page transitions. I have attempted (unsuccessfully) both a Widget and Ember Component, but neither are updating. I loaded them via a Plugin Outlet. I even tried to simplify the problem and dump the current timestamp of the load to the screen (rather than a banner), but only get a re-render if I refresh the browser.

Can someone please point me in the right direction for a solution? I’ve been through the main Discourse developer tutorials, BTW. Just seems like I’m missing something simple.

In which place you are trying to render? In Discourse most of the header UI sections will not refresh across page transactions unlike you hit browser refresh.

And can you post your code to see how you are doing it.

The plugin outlet right above the topic list.

Maybe you can try the following code (not tested):

// My plugin initializer
export default {

	name: 'myPlugin',

	initialize(container) {

		// Reopen the router to add a didTransition hook
		const router = container.lookup('router:main')
		router.reopen({
			doSomething: function() {
				// Wait for rendering to be over. THIS DOESN'T WORK IN ALL CASES. 
				// In fact, there is no way to be sure rendering is complete, because 
				// a 'complete rendering' is meaningless (think about a looping DOM  
				// animation)
				Ember.run.schedule('afterRender', null, () => {
					// Do your stuff here
					...
				})
			}.on('didTransition')
		})

	}

}

有人能进一步指导一下吗?

您能解释一下您想要实现什么吗?

需要在页脚区域添加一个组件,并在用户访问每个主题时,在每次页面加载时更新其内容。这是一个轮播图,包含我们社区的热榜主题,仅对匿名用户在主题页面可见。因此,当用户访问轮播图中存在的每个主题时,该幻灯片需要从组件中移除。使用下方的页脚出口挂载它并不会重新渲染组件。

当 Discourse 渲染“页脚”时,应用会发送一个“事件”。

该事件为 inserted-custom-html:footer

您可以使用此事件来更新页脚内容,方法如下:

api.onAppEvent("inserted-custom-html:footer", () => {
  // 在此处添加您的 JS 代码
});

这里还有一些更多细节 在此处。

这应该能实现您想要的效果。如果不行,请告诉我,并附上您正在使用的代码。

2 个赞

我希望在主页顶部(仅限主页)放置动态内容。

目前有什么最佳实践可以让它在每次过渡到主页时重新渲染?

如果您能提供任何指向现有插件的链接,我将不胜感激。