英文:
Always render certain Wicket HeaderItem after all other HeaderItems of specific type
问题
我有一个自定义的 Wicket 行为(RegisterBehaviour
),可以附加到各种 Wicket 组件上,通过在 RegisterBehaviour.renderHead(Component, IHeaderResponse)
中使用(OnDomReady-)HeaderItem 来为它们添加多个 JavaScript 片段事件处理程序。
现在我有一个要求,即在页面加载后立即触发其中一个事件,以使用特定计算值初始化所有组件。为此,我认为可以添加另一个 Wicket 行为(TriggerBehaviour
),用于生成适当的 JavaScript 片段。
然而,我的问题是,这个 TriggerBehaviour
生成的 JavaScript 片段必须在页面上的所有 RegisterBehaviour
的 JavaScript 片段以及所有组件中的代码之后运行。我知道 HeaderItem 可以具有依赖关系,但这个机制似乎是针对当我想要加载某种常见库(比如 jQuery)或在自定义代码之前执行代码的情况。要将其用于我的情况,我需要在当前页面渲染阶段动态查找所有正在添加到其中的RegisterBehaviours
,并将它们动态添加为TriggerBehaviour
的依赖项,以便它们在TriggerBehaviours
JavaScript 之前运行。然而,我看不出如何获取特定类型的所有 HeaderItem 的列表。
关于如何在 Wicket 或 JavaScript 中解决这个问题,有什么建议吗?
(使用 Java 8 和 Wicket 8.9.0)
英文:
I have a custom Wicket Behaviour (RegisterBehaviour
) which can be attached to various Wicket components to add multiple JaveScript-Snippets event handlers to them via a (OnDomReady-)HeaderItem in RegisterBehaviour.renderHead(Component, IHeaderResponse)
.
Now I have the requirement to trigger one of these events right after the page has been loaded to initialise all components with a certain computed value. To do that, I thought could add another Wicket Behaviour (TriggerBehaviour
) which generates the approriate JaveScript-Snippet.
My problem is though, that the JaveScript-Snippet generated by this TriggerBehaviour
would have to be run after the JaveScript-Snippets of all the RegisterBehaviour
on a page and in all components have been run. I know that HeaderItems can have dependencies but this mechanism seems to target the case, when I want some kind of common library loaded (like jQuery) or code executed before my custom code. To use this for my case, I'd have to somehow find all RegisterBehaviours
on the fly that are being added to the current render phase of the page and dynamicly add them as dependencies to the TriggerBehaviour
, so they run before the TriggerBehaviours
JavaScript. I see no way though, how to get such a list of all HeaderItems of a certain type.
Any tips on how to approach this problem, either in Wicket or JavaScript?
(Using Java 8 and Wicket 8.9.0)
答案1
得分: 1
你可以使用FilteredHeaderItem。
RegisterBehaviour和TriggerBehaviour都会贡献它们的标题项,这些标题项被包装在FilteredHeaderItem
中。
在你的HTML模板中,你将为这两个过滤器使用两个占位符:
<div id="registerFilter"></div>
<div id="triggerFilter"></div>
浏览器按从上到下的顺序执行JavaScript,因此触发器部分将会在注册部分之后。
英文:
You can use FilteredHeaderItem.
Both RegisterBehaviour and TriggerBehaviour will contribute their header items wrapped in FilteredHeaderItem
s.
In your HTML template you will use two placeholders for the two filters:
<div id="registerFilter"></div>
<div id="triggerFilter"></div>
The browsers execute the JavaScripts from top to bottom so the trigger ones will be after the register ones.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论