1 #!/usr/local/bin/node
  2 
  3 /**
  4  * @fileoverview  To get started with nclosure you will need to do the
  5  *  following:
  6  * <ul>
  7  *  <li>Load nclosure through Node's standard mechanism
  8  *    <p>i.e. [var nclosure = require('nclosure')].</p>
  9  *  </li><li>Initialise nclosure:
 10  *    <p>i.e. [require('nclosure').nclosure(options)]</p>
 11  *    <p>
 12  *    <strong>Note: </strong>Before initialisation no closure librarues can be
 13  *      used.
 14  *    </p><p>
 15  *    The nclosure() method takes in an optional options object with the
 16  *    following properties:
 17  *    </p>
 18  *    <pre>
 19  *    {
 20  *       // Location of the closure-library directory
 21  *       closureBasePath:{string},
 22  *       // Any additional dependency files required to run your code.  These
 23  *       // files generally point to other closure libraries.  Note these deps
 24  *       // files must have paths relative to the same closureBasePath as
 25  *       // specified above
 26  *       additionalDeps:{Array.<string>},
 27  *       // Path to the compiler jar you want to use.  Default: 'compiler.jar'.
 28  *       compiler_jar: {string},
 29  *       // Additional compiler options, e.g: --jscomp_warning=newWarningType
 30  *       additionalCompileOptions: {Array.<string>},
 31  *       // These are directories containing source code that needs to be
 32  *       // included in the compilation.  If this is not included then
 33  *       // additionalDeps is used to try to guess any additional roots
 34  *       // required (assumes that the deps.js file is in the root folder of
 35  *       // the source directory).
 36  *       additionalCompileRoots: {Array.<string>}
 37  *    }
 38  *    </pre>
 39  *  </li><li>
 40  *    You can also place a closure.json file in the root directory of your
 41  *    source code that will allow you to call the init method with no
 42  *    parameters.  Using a closure.json is highly recommended as this will
 43  *    allow you to use other tools such as jsdoc-toolkit and give you more
 44  *    configuration over other settings.  See the example closure.json file
 45  *    included in this directory.
 46  *  </li><li>Use closure library depenencies as required
 47  *    <p>i.e. [goog.require( 'goog.async.Delay' )].</p>
 48  * </li></ul>
 49  * @see closure.json
 50  */
 51 
 52 /*
 53  * Does not require an opts parameter as we are providing all the options in
 54  * the closure.json file in this directory;
 55  */
 56 require('nclosure').nclosure();
 57 
 58 /*
 59  * Now that the nclosure is initialised you can use any base.js functionality
 60  * such as goog.require / goog.provide
 61  */
 62 goog.require('goog.async.Delay');
 63 goog.require('goog.structs.Trie');
 64 goog.require('nclosure.external.Utils');
 65 
 66 // At least one namespace must be provided for compilation purposes
 67 goog.provide('nclosure.examples.simple.Example');
 68 
 69 
 70 
 71 /**
 72  * Example of how to use nclosure project.  The nclosure project allows you
 73  * to levarage google's closure compiler and libraries to provide you with a
 74  * rich set of tools and type-safety not found in any other JavaScript stack.
 75  * This example aims to demonstrate how to use the nclosure tool not teach
 76  * you the basics of google closure.  For more information on google closure
 77  * tools see the Closure Tools project documentation.
 78  *
 79  * @see <a href="http://code.google.com/closure/">Closure Tools</a>.
 80  * @constructor
 81  */
 82 nclosure.examples.simple.Example = function() {
 83   /**
 84    * Wether the timer is finnished
 85    * @type {boolean}
 86    */
 87   this.completed = false;
 88 
 89   this.createDelay_();
 90   this.testTrie_();
 91   this.testExternalLib_();
 92 };
 93 
 94 
 95 /**
 96  * Create a delayed function which will be executed in 1.5 seconds.
 97  *
 98  * @private
 99  */
100 nclosure.examples.simple.Example.prototype.createDelay_ = function() {
101   new goog.async.Delay(function() {
102     console.info('Bye!');
103     this.completed = true;
104   }, 300, this).start(300);
105 };
106 
107 
108 /**
109  * Create a trie and insert some data. A trie finds the associated data of all
110  * prefixes (of 'examples' in this case) in O(L), where L is the length of
111  * the key:
112  *
113  * @private
114  */
115 nclosure.examples.simple.Example.prototype.testTrie_ = function() {
116   var trie = new goog.structs.Trie();
117   trie.add('demo', 'nclosure');
118   trie.add('ex', ['girlfriend', 'parrot']);
119   trie.add('example', { 'hello': 'world' });
120 
121   console.info(trie.getKeyAndPrefixes('examples'));
122 };
123 
124 
125 /**
126  * Tests link to an external library (using additionalDeps option)
127  *
128  * @private
129  */
130 nclosure.examples.simple.Example.prototype.testExternalLib_ = function() {
131   console.info('Using nclosure.external.Utils.echo("hello world"): ' +
132       nclosure.external.Utils.echo('hello world'));
133 };
134 
135 
136 /**
137  * A reference to the running example for testing purposes
138  * @type {nclosure.examples.simple.Example}
139  * @private
140  */
141 var example_ = new nclosure.examples.simple.Example();
142