本文介绍了使用带有jQuery的Google Closure模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



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



但在查看之后,我看到两者都与每个问题进行了比较其他它让我想知道几件事情。


  1. 如果我按照我的想法去做,那么我能够在我的jQuery小部件中调用Google Closure生成的函数来呈现UI吗?


  2. 如果我离开jQuery并且只使用Google Closure就足够了根据我的要求?


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


On 2 and 3如果任何已经使用它的人都可以提供一些见解,那就太棒了。



注意: - 如果它有任何相关性,我们正在为服务器端的Microsoft .NET堆栈工作。

解决方案

Closure库和Closure模板不依赖于彼此,因此您当然可以使用带有jQuery的Closure模板而无需拉入Closure库或Closure Compiler。要使用带有jQuery的模板,请使用 SoyToJsSrcCompiler.jar 。然后,每个Soy文件都有一个JavaScript文件,其中每个JavaScript文件在相应的Soy文件中定义每个模板一个函数。



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




  • soyutils.js

  • 从Soy生成的JavaScript

  • jQuery库

  • 您的应用程序代码,可能取决于jQuery和你的模板函数。



快速了解Closure模板比学习库或编译器容易得多所以我确信你的开发团队可以快速拿起它。我相信是彻底的,不会让人不知所措,因此语法和用法不会花费太长时间学习。



请注意,如果您决定在某些时候使用Closure Library而不是jQuery,则应该包含而不是。虽然如果您决定在拥有大量代码后重写应用程序逻辑以依赖Closure Library而不是jQuery,那么这个小小的改变可能是您最不关心的问题!也就是说,我确信您最终可以使用Google Closure编写整个应用程序,但是对于大型应用程序从一个JavaScript库迁移到另一个JavaScript库可能需要进行如此多的代码更改,以至于您可能无法进行迁移。 / p>

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.

解决方案

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.

这篇关于使用带有jQuery的Google Closure模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 21:34
查看更多