如何自动组织自定义 Facelets 组件使用的脚本?

发布于 2024-11-06 04:30:51 字数 1717 浏览 0 评论 0原文

例如,我创建了一些自定义 UI 组件,有些依赖于 jquery-min.js,有些依赖于 jQuery UI,有些依赖于 jsTree >,有些依赖Dojo等。

我可以在每个xhtml文件中导入所需的库:

file1.xhtml
<script src="jquery-min.js"></script>
<script src="jquery-ui.js"></script>
<script src="jsTree.js"></script>
<my:tree> ... </my:tree>

file2.xhtml
<script src="jquery-min.js"></script>
<script src="jquery-ui.js"></script>
<script src="jquery-ui-dialog.js"></script>
<script src="jquery-ui-autocompletion.js"></script>
<my:autodialog> ... </my:autodialog>

file3.xhtml
<script src="jquery-min.js"></script>
<script src="dojo.js"></script>
<my:dojostuff> ... </my:dojostuff>

这很不方便,我必须知道哪个组件依赖于哪个库,以及依赖项的依赖关系。

将所有依赖项放在模板文件中肯定会让事情变得更简单,但这会加载太多脚本,并占用手机内存。

那么,是否有类似“UIAutoOrganizedScriptsComponent”的东西?

我是否应该使用类似请求范围的哈希集之类的东西,其中包含当前请求中使用的组件填充的依赖项,如下所示?

static ThreadLocal<HashSet> requestScopedDependencies;
static Map<String, URL> libraryURLs;

MyTreeComponent extends UIOutput {
    // ...
    dependencies = requestScopeDependencies.get();
    dependencies.add("jstree");
}

或者,也许我应该搜索 Component DOM 以找出可以在哪里注入

MyTreeComponent extends UIOutput {
    // ...
    UIComponent scriptsContainer = getParent().getParent().getChildren()[2].getChildren()[3];

    // search if scriptsContainer already included the script...
    if (! included) {
        scriptsContainer.addElement("<script>...");
    }
}

嗯,非常笨拙。有什么优雅的解决方案可以做到这一点吗?

For example, I have created some custom UI components, some depend on jquery-min.js, some depend on jQuery UI, some depend on jsTree, some depend on Dojo, etc.

I can import the required libraries in each xhtml files:

file1.xhtml
<script src="jquery-min.js"></script>
<script src="jquery-ui.js"></script>
<script src="jsTree.js"></script>
<my:tree> ... </my:tree>

file2.xhtml
<script src="jquery-min.js"></script>
<script src="jquery-ui.js"></script>
<script src="jquery-ui-dialog.js"></script>
<script src="jquery-ui-autocompletion.js"></script>
<my:autodialog> ... </my:autodialog>

file3.xhtml
<script src="jquery-min.js"></script>
<script src="dojo.js"></script>
<my:dojostuff> ... </my:dojostuff>

This is very inconvenient, I have to know which component depends on which library, and the dependencies of the dependencies.

To put all dependencies in the template file will certainly make things simpler, but that will load too much scripts, and memory out in mobile phone.

So, is there something like "UIAutoOrganizedScriptsComponent"?

Should I use something like request-scoped hash set, which contains the dependencies filled by components used in the current request, like this?

static ThreadLocal<HashSet> requestScopedDependencies;
static Map<String, URL> libraryURLs;

MyTreeComponent extends UIOutput {
    // ...
    dependencies = requestScopeDependencies.get();
    dependencies.add("jstree");
}

Or, maybe I should search the Component DOM to find out where I can inject the <script>? like this:

MyTreeComponent extends UIOutput {
    // ...
    UIComponent scriptsContainer = getParent().getParent().getChildren()[2].getChildren()[3];

    // search if scriptsContainer already included the script...
    if (! included) {
        scriptsContainer.addElement("<script>...");
    }
}

Well, very clumsy. Is there any elegant resolution to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

箜明 2024-11-13 04:30:51

听起来您需要类似 RequireJS 的东西来在页面上动态加载依赖项。

It sounds like you need something along the lines of RequireJS to load the dependencies dynamically on the page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文