Theme 组件的 j/s 执行顺序由什么控制?

I have j/s in my theme (am I infamous yet? ;) which is modifying the DOM by adding elements via api.decorateCooked(…. It inserts a variety HTML head elements.

I also have the DiscoTOC Theme component installed.

However, adding the usual <div data-theme-toc="true"> </div> that DiscoTOC notices and expands, doesn’t generate a table-of-contents including my dynamically inserted elements.

I’m figuring this might be as simple as execution order… if DiscoTOC looks before my j/s adds more elements . . .

What determines the ordering of execution if multiple bits of theme j/s code are calling api.cooked(?

3 个赞

bump? :slight_smile:

plus silliness to get to 20 characters

Note that DiscoTOC only runs on post contents - if you’re inserting those elements outside posts, it won’t see them.

I don’t think we’ve needed to define JS execution order yet.

1 个赞

I’m inserting within the post, yes. That’s why I suspect execution order.

Some bit of code determines the execution order… that’s the leverage point for me to get my component before TOC. eg, if it’s alpha by component name (ie, as displayed in the Component list in Admin area) then I can try namin my component accordingly, etc

Pretty sure that the component execution order is not based on the name of the component. It’s based on the ID by default.

That said, you can set initializer order for advanced use cases. I can put DiscoTOC in a named initializer so you can run your code before DiscoTOC, but I need to know what you’re trying to do first to give you a clear answer.

Your modifications here are based on an HTTP/AJAX request, correct?

1 个赞

Yes AJAX, but using a synchronous call. (I’ve always loved the oxymoron of synchronous AJAX :^)

I’m feeling like my next step is create a proper component—atm, it’s just some j/s pasted into my Theme’s Header. Then I can try fiddling with a judiciously chosen ID. “_foo” or “0-foo” might be all I need.

1 个赞

Don’t we all :wink:

Correct. If you want to use named initializers and initializer order, your component will need to have an initializer in the /javascript folder.

No worries, though. I can help with all of that. I’ll get back to you on this tomorrow with a full answer.

1 个赞

不久前,我将我的 j/s 破解改造成了一个主题组件。

我很想回到让我的组件在 Discourse table-of-contents 之前运行的排序问题上。

我记得,主题组件是在插件之后进行评估的。

可能记错了,但它们是按名称顺序加载的,所以在名称前加上 x- 过去曾帮助我确保特定的主题组件最后被评估。

没关系。我正在尝试调整 DiscoTOC(https://github.com/discourse/DiscoTOC.git)组件和我自己创建的组件的顺序。

其他人的回复表明组件的_名称_不会影响执行顺序。我刚刚将它们重命名为 aa-mycomponent 和 x-last-DiscoTOC,但它不起作用。[DiscoTOC 不包括动态添加到 DOM 的标题——是的,在帖子正文中]

https://forum.moversmindset.com/t/list-of-movers-mindset-podcast-episodes/1160

@Johani 从你五月份的思路中还有其他想法吗?

hmmmmmmm… 也许问题不仅仅是排序。我刚刚检查了一下……我以为是 HTML 标题的东西在 DOM 中实际上是带有 class=\"d-toc-ignore\" 的 DIV 标签。

是时候深入挖掘了。我的 标题 去哪儿了? :slight_smile:

嗯,我认为你可以通过尝试一个非常简单的例子来证明这一点。

创建两个简单的组件,覆盖或连接同一个东西,也许是一个带有细微差别的简单模板,给其中一个起一个字母表中靠后的名字,注意哪个组件最终会覆盖主题。

这里的代码非常清楚地按名称排序:

1 个赞

名称不影响执行顺序。@merefield 您链接的行会影响它们在管理员 UI 中的显示方式,但并不用于实际执行。

这是与执行顺序相关的行:

因此,它们将按数据库中“id”的升序执行。

在 Discourse 实例之间可移植的唯一保证是,主题代码始终在主题组件代码之前执行。

正如 @Johani 在上面提到的,如果您需要在使用其他组件加载后运行代码,最好的方法可能是使用 Ember initializer,它支持排序。不幸的是,在这种情况下,这可能无法立即实现,因为 DiscoTOC 似乎没有使用 Ember initializer。将 DiscoTOC 更新为使用多文件 JavaScript(因此是 Ember initializer)当然是 #pr-welcome。

3 个赞

这很不幸,因为它可能很有用。无论如何,感谢您的澄清!

1 个赞

(……这与 Theme component order of precedence 相关)

只是回来再说一下,数据库 ID 的优先级确实有效;解决了我的需求。谢谢大家!

2 个赞