Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

confusing google closure library api

could someone explain to me how the Closure works in more user-friendly form? Its help and documentation leads me nowhere really. How do you perform a simple task such as selecting and modifying the dom (e.g. select all on page and hide them)?

like image 230
jirkap Avatar asked Oct 03 '10 17:10

jirkap


2 Answers

See http://derekslager.com/blog/posts/2010/06/google-closure-introduction.ashx, Comparison #4,

Hide all div's:

<html>
<head>
<script src="http://closure-library.googlecode.com/svn/trunk/closure/goog/base.js" type="text/javascript"></script>
<script language="JavaScript">
  goog.require('goog.dom.query');
  goog.require('goog.style');
</script>
<script> 
  function HideElement(selector) {
    goog.array.map(goog.dom.query(selector, null), function(e) { 
        goog.style.showElement(e, false); 
    });
  }
</script>
</head>
<body>
   <div>div</div>
   <p>paragraph</p>
   <div>another div</div>
   <input type="button" value="hide" onclick="HideElement('div');"/>
</body>
</html>

Can't help you with the user-friendly breakdown, though.

like image 105
user194743 Avatar answered Nov 08 '22 04:11

user194743


I thought the API docs were really great at first, but after writing a few hundred lines of code I've run into all kinds of quirks and problems. For instance the dom module documentation doesn't have a clear entry point for discovering dom manipulation methods -> all the top-level links are to helper objects it uses internally. You can find some useful methods though if in the package reference list you click dom, then DomHelper. It seems like you need to instantiate a DomHelper to get access to those tools though?

Luckily they did include handy links into the code all over the API docs. If you poke around in the DomHelper source you'll see that most of the listed methods are available directly from the goog.dom namespace!

My other major gripe is that the docs often don't list argument types/names/descriptions. For instance if you expand goog.dom.DomHelper.contains it doesn't list any arguments, but the code correctly annotates two args. I can't believe they made such a thoroughly annotated and documented library and then failed to include that information in the (generated) docs! Although while browsing their code you will often find terse and uninformative comments in their annotations too.

So, to summarize: read the code! I always hate hearing that answer but it seems to be the best option right now.

I have the O'Reilly Closure book too, and although it does provide some insights it still isn't very in-depth on actually using basic patterns and tools provided in the library. I would really like a better over-view of how parts of the library are intended to interact. I guess someone should make a closure-tools cookbook?

like image 34
kitsu.eb Avatar answered Nov 08 '22 04:11

kitsu.eb