将 soyutils 与 Google Closure 结合使用的正确方法是什么?

发布于 2024-12-14 22:46:19 字数 776 浏览 11 评论 0原文

我正在尝试将 Google Closure 模板 (Soy)Google 关闭

我按照指示添加了 soyutils_usegoog.js 实用程序文件。该文件提供了生成的模板使用的许多实用程序,特别是 soy.StringBuilder。下面是它的创建方式:

soy.StringBuilder = goog.string.StringBuffer;

soyutils 文件需要上面几行 goog.string.StringBuffer ,但是当在非编译模式下运行时,这会导致运行时错误,因为 StringBuffer 所在的位置在 soyutils 执行之后之后才会被加载。

除非我弄错了,否则 Closure 中的 JS 文件不应立即访问它们“需要”的名称空间。

简而言之,如何加载 soyutils_usegoog.js 而不会由于 good.string.StringBuffer 的早期访问而触发运行时错误。

I am trying to use Google Closure templates (Soy) with Google Closure.

I am including the soyutils_usegoog.js utilities file as instructed. This file provides a number of utilities used by the generated templates, notably soy.StringBuilder. Here's how it is creating it:

soy.StringBuilder = goog.string.StringBuffer;

The soyutils file requires goog.string.StringBuffer a few lines above, but when running in non-compiled mode this results in a runtime error because the JS file that StringBuffer resides in will not be loaded until after soyutils has executed.

Unless I am mistaken, JS files in Closure should not immediately access namespaces that they 'require'. The <script> tag is only added after the execution of the current script (in non-compiled mode) so immediate usage will result in a runtime error.

In short, how can I load in soyutils_usegoog.js without triggering a runtime error due to the early access of good.string.StringBuffer.

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

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

发布评论

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

评论(2

攒眉千度 2024-12-21 22:46:19

您是否使用已编译的模板作为输入(以及 soyutils_usegoog.js)重新创建 deps.js?然后您是否使用了 goog.require 模板?像这样的东西应该有效:

<script src="/closure-library/closure/goog/base.js"></script>
<script src="/closure-library/closure/goog/deps.js"></script><!-- might not need this line if base.js is setup to auto include deps.js -->
<script>
    goog.require('your.template');//this will pull in and execute all the dependencies (including StringBuffer) for your template
</script>
<script>
    alert(your.template());
</script>

Are you recreating deps.js with your compiled templates as an input (and soyutils_usegoog.js)? And then are you goog.require-ing your template? Something like this should work:

<script src="/closure-library/closure/goog/base.js"></script>
<script src="/closure-library/closure/goog/deps.js"></script><!-- might not need this line if base.js is setup to auto include deps.js -->
<script>
    goog.require('your.template');//this will pull in and execute all the dependencies (including StringBuffer) for your template
</script>
<script>
    alert(your.template());
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文