Closure The definitive guide, help needed for the most basic example

254 views
Skip to first unread message

julien

unread,
Nov 3, 2011, 4:44:47 PM11/3/11
to Closure Library Discuss
Hi All,
I started reading Closure The Definitive Guide, which really makes me
want to discover and use closure's features,
However, for some reason the second example, at page 17 in the book
are giving me a bit of a headache, each time I include the
soyutils_usegoog.js file from the closure-templates project,
I get errors saying

goog.require could not find: goog.soy
base.js:287Uncaught Error: goog.require could not find: goog.soy

goog.requirebase.js:287
(anonymous function)

I tried, plovr, checked the simple demo on the site and got almost the
same code to work using plovr, the issue is that I'd like to just
include the js files because when Im running plovr (or any java app)
on some of my crappy laptops, time plovr compiles the script, let's me
enough time to go grab a coffee and come back,

So my first thought is that the library probably has changed, but I
grabbed the library from svn instead and same with the templates,

including the following in my html document gives me the same error :

<script src="../js/closure-library/closure/goog/base.js" type="text/
javascript"></script>
<script>
goog.require('goog.string.StringBuffer');
</script>
<script src="../js/closure-templates/soyutils_usegoog.js" type="text/
javascript"></script>

... no extra code
So if anyone can help me getting this work, it would be appreciated

Cheers

Chris Price

unread,
Nov 4, 2011, 3:41:18 AM11/4/11
to closure-lib...@googlegroups.com

Not really sure as I'm not familiar with the example, but here's a few things off the top of my head -

Could it be you're missing deps.js?

Not sure you have the latest copy of the library as base.js:287 is a comment - http://code.google.com/p/closure-library/source/browse/trunk/closure/goog/base.js

Also from the code you pasted in nothing appears to be goog.require-ing goog.soy so I assume there's more to your example?

Julien Castelain

unread,
Nov 4, 2011, 3:55:06 AM11/4/11
to Closure Library Discuss
Hi Chris,

I just grabbed the latest source from svn, (for both closure-library
and closure-templates)

Here is the 'example' code I'm using, which is still failing for me, I
included the deps.js file as suggested, as you can see there is no
extra code,


<!DOCTYPE html>
<html lang="en">
<head>
<title></title>

</head>
<body>
<div id="hello"></div>

<script src="../js/closure-library/closure/goog/base.js"
type="text/javascript"></script>
<script src="../js/closure-library/closure/goog/deps.js"
type="text/javascript"></script>
<script>
goog.require('goog.string.StringBuffer');
</script>
<script src="../js/closure-templates/soyutils_usegoog.js"
type="text/javascript"></script>
</body>
</html>

I still get this error

goog.require could not find: goog.soy
goog.requirebase.js:284
base.js:287Uncaught Error: goog.require could not find: goog.soy

Testing in Chrome, (dev channel)
Can you tell me if this works for you?

Thanks a lot
Julien




On Nov 4, 8:41 am, Chris Price <pric...@gmail.com> wrote:
> Not really sure as I'm not familiar with the example, but here's a few
> things off the top of my head -
>
> Could it be you're missing deps.js?
>
> Not sure you have the latest copy of the library as base.js:287 is a
> comment -http://code.google.com/p/closure-library/source/browse/trunk/closure/...
>
> Also from the code you pasted in nothing appears to be goog.require-ing
> goog.soy so I assume there's more to your example?

Chris Price

unread,
Nov 4, 2011, 5:41:47 AM11/4/11
to closure-lib...@googlegroups.com
Changing it to the following works for me (you can ignore the path changes) -

<!DOCTYPE html>
<html lang="en">
 <head>
   <title></title>

 </head>
 <body>
   <div id="hello"></div>

   <script src="closure-library-read-only/closure/goog/base.js"
type="text/javascript"></script>
   <script src="closure-library-read-only/closure/goog/deps.js"
type="text/javascript"></script>
   <script>
     goog.require('goog.asserts');
     goog.require('goog.dom.DomHelper');
     goog.require('goog.format');
     goog.require('goog.i18n.BidiFormatter');
     goog.require('goog.i18n.bidi');
     goog.require('goog.soy');
     goog.require('goog.string');
     goog.require('goog.string.StringBuffer');
   </script>
   <script src="closure-templates-read-only/javascript/soyutils_usegoog.js"
type="text/javascript"></script>
 </body>
</html>

The problem is that you need to define any dependencies for a file before it's loaded. That's where deps.js comes in, it is just a big list of dependencies. If the soyutils_usegoog was defined in the deps.js file you could have just goog.require-d it and it should have worked. However, because it's part of a different project it's not in the closure lib deps file.

The above fix works but as you learn more about how the require/provide stuff works you'll find much better ways of doing it.

julien

unread,
Nov 4, 2011, 7:47:56 AM11/4/11
to Closure Library Discuss
Thanks Chris,

Changing it to what you suggested just give me 2 more errors ...

If I just include base.js, and then try to require 'goog.soy'
It just breaks

I really wonder what's wrong in my code

Cheers

julien

unread,
Nov 5, 2011, 4:48:19 AM11/5/11
to Closure Library Discuss
My Bad, I was testing this with the .'zipped' version of the closure-
library, but with the source from svn it does work

Thanks for the help
Cheers

Michael Bolin

unread,
Nov 8, 2011, 10:18:41 AM11/8/11
to Closure Library Discuss
Hi Julien, for plovr, how slow is "slow"? And have you tried using RAW
mode and/or giving Java more memory with -Xmx256M or somesuch?

--Michael

julien

unread,
Nov 10, 2011, 12:38:56 AM11/10/11
to Closure Library Discuss
Hi Michael,

I haven't tried RAW mode yet, but I will ...
With JAVA_OPTS set to -Xms128m -Xmx512m -XX:MaxPermSize=512m
it compiles in about 45 secs. on this machine (this might be a bad
example since it's an asus eeepc with 2048m of RAM)
I'll check RAW mode

Thanks
Julien

julien

unread,
Nov 10, 2011, 12:43:07 AM11/10/11
to Closure Library Discuss
Hi again,

With RAW mode it is much faster on the same machine, around 5 secs
Thanks for the heads up

Julien

John Szwaronek

unread,
Feb 11, 2012, 8:25:19 PM2/11/12
to closure-lib...@googlegroups.com
I'm a newcomer to Closure but I believe the following is correct.

Once goog/base.js is inserted, it automatically inserts goog/deps.js . So you only need the first line of the following:
<script src="closure-library-read-only/closure/goog/base.js" type="text/javascript"></script>
<script src="closure-library-read-only/closure/goog/deps.js" type="text/javascript"></script>

Anyway that's what Webstorm IDE and Firebug seem to show. My apologies if I'm wrong.

As an aside it seems you can also use Templates without using the Closure Library. This may be useful for legacy code and the like.
1. Run SoyToJsSrcCompiler.jar without the --shouldProvideRequireSoyNamespaces so goog.provide and .require stmts are not generated.
2. The only additional script to include in the HTML is the .soy.js Templates file(s) and
<script src="../ClosureTemplates/soyutils.js"></script> 
This inserts the runtime version which does not use of the Library.
3. I ran a primitive test which suggests the above may work even if you are using the library elsewhere.
Reply all
Reply to author
Forward
0 new messages