将 Google Closure 模板与 jQuery 结合使用

发布于 2024-11-27 02:07:40 字数 1005 浏览 1 评论 0原文

我们开始使用 JavaScript 和 HTML5 创建一个应用程序,该应用程序将使用 REST API 来访问服务器资源,利用我们的开发团队已经熟悉的 jQuery 的强大功能和易用性。该应用程序将成为多语言版本。后来我们决定使用 JavaScript 编写 DOM,这将使我们能够灵活地使用 UI 位与其他应用程序集成,并使用 jQuery UI 小部件创建我们自己的小部件。然后,只需在其他应用程序的相关页面中添加引用 JavaScript 文件的脚本标记,我们就可以完成该特定功能的大部分集成。

因为使用 JavaScript 创建 DOM 需要大量编码,所以我们开始寻找能够帮助我们轻松将 HTML 转换为 JavaScript 以创建 UI 的工具,因此 Google Closure Template 出现了。

此时我想到的是,使用 Google 闭包来编写 UI DOM 位,因为它可以快速为我的 DOM 提供 JavaScript,然后为其他 JavaScript(即用于服务器端通信和其他 UI 逻辑,例如一旦从服务器获得响应就更改 UI,并且 x 应该更改)点击 z 之类的东西到 y)需要手写,我应该使用jQuery,它很容易写。

但是在查看这个问题之后,我发现两者都是相互比较的,这让我对一些事情感到疑惑。

  1. 如果我按照我的想法去做,我是否能够在我的 jQuery 小部件中调用 Google Closure 生成的函数来渲染 UI?

  2. 如果我离开 jQuery 并仅使用 Google Closure,它是否足以满足我的要求?

  3. 当我开始阅读 Google Closure 文档时,我发现它有自己的全新世界,并且涉及学习曲线。多少钱?如果不是很多,那么我们的 5 名开发人员团队将准备好学习它。

关于2和3,如果已经使用过它的人能够提供一些见解,那就太好了。

注意:- 以防万一它有任何相关性,我们正在研究服务器端的 Microsoft .NET 堆栈。

We are starting to create an application using JavaScript and HTML5 which will use rest API to access server resources taking the advantage of jQuery awesomeness and easiness which our dev team is already comfortable with. This application is going to be made multilingual. We had decided later that we will write our DOM using JavaScript which will allow us the flexibility to use our UI bits for integration with our other applications and will create our own widgets using jQuery UI widgets. Then by just adding a script tag referencing JavaScript file in a relevant page of our other application, we will have most of our integration for that particular feature done.

Because it takes lot of amount of coding for creating DOM using JavaScript, we started looking in search of tools which will help us easily convert HTML to JavaScript for creation of UI and hence Google Closure Templates came in.

At this time what I thought of was, using Google closure for writing the UI DOM bit as it can quickly give me JavaScript for my DOM and then for other JavaScript (i.e. for server side communication and for other UI logic like changing of UI once got response from the server and x should change to y on click of z kind of things) which needs to be handwritten, I should use jQuery which is easy to write.

But after looking at this question, I see that both are compared against each other and it left me wondering on few things.

  1. If I go by what I've thought of doing then, will I be able to call the functions generated by Google Closure in my jQuery widgets to render the UI?

  2. If I leave jQuery and just use Google Closure will it be enough for my requirements?

  3. As I started reading Google Closure documentation, I found that it has a whole new world of it's own and learning curve is involved. How much it is? If it is not much, then our team of 5 devs will be ready to learn it.

On 2 and 3, it would be great if anyone who has already used it can provide some insight.

Note:- Just in case if it has any relevance, we are working on Microsoft .NET stack for server side.

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

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

发布评论

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

评论(1

肩上的翅膀 2024-12-04 02:07:40

闭包库和闭包模板并不相互依赖,因此您当然可以将闭包模板与 jQuery 结合使用,而无需引入闭包库或闭包编译器。要在 jQuery 中使用模板,您可以使用 SoyToJsSrcCompiler.jar 如文档中所述。然后,每个 Soy 文件都会有一个 JavaScript 文件,其中每个 JavaScript 文件都包含相应 Soy 文件中定义的每个模板的一个函数。

要使用生成的 JavaScript 函数,您还必须包含 soyutils.js,这是生成的函数所需的一组实用程序。因此,您的生产系统应包含以下按顺序连接/缩小的 JavaScript 文件:

  • soyutils.js
  • 从 Soy
  • jQuery 库
  • 生成的 JavaScript您的应用程序代码,可能依赖于 jQuery 和您的模板函数。

快速掌握闭包模板比学习库或编译器要容易得多,因此我确信您的开发团队可以快速掌握它。我相信在线文档很全面,但不会让人不知所措,因此语法和用法不会花很长时间学习。

请注意,如果您决定在某个时候使用 Closure 库而不是 jQuery,则应包含 soyutils_usegoog.js 而不是 soyutils.js。不过,如果您在拥有大量代码后决定重写应用程序逻辑以依赖 Closure Library 而不是 jQuery,那么这个小更改可能是您最不需要担心的!也就是说,我确信您最终可以使用 Google Closure 编写整个应用程序,但是对于大型应用程序从一个 JavaScript 库迁移到另一个 JavaScript 库可能需要进行大量代码更改,以至于您可能不敢进行迁移。

Closure Library and Closure Templates do not depend on one another, so you can certainly use Closure Templates with jQuery without pulling in the Closure Library or the Closure Compiler. To use Templates with jQuery, you translate your Closure Template files (aka "Soy" files) using SoyToJsSrcCompiler.jar as described in the documentation. Then you will have one JavaScript file for each Soy file where each JavaScript file contains one function per template defined in the corresponding Soy file.

To use the generated JavaScript functions, you must also include soyutils.js, which is a set of utilities required by the generated functions. Therefore, your production system should include the following JavaScript files concatenated/minified in this order:

  • soyutils.js
  • JavaScript generated from Soy
  • jQuery library
  • Your application code, which presumably depends on both jQuery and your template functions.

Getting up to speed on Closure Templates is considerably easier than learning the Library or the Compiler, so I'm sure that your dev team can pick it up quickly. I believe the online documentation is thorough without being overwhelming, so the syntax and usage should not take long to learn.

Note that if you decide to use the Closure Library instead of jQuery at some point, you should include soyutils_usegoog.js instead of soyutils.js. Though if you decide to rewrite your application logic to depend on Closure Library instead of jQuery after you have a substantial amount of code, this small change will likely be the least of your concerns! That is, I'm sure you could ultimately write your entire application using Google Closure, but migrating from one JavaScript library to another for a large application will likely require so many code changes that you may be too intimidated to take on the migration.

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