The Golf Web Application Server home roadmap1.0 help rss

JavaScript Preprocessor Hooks

22 Feb 2010 - Micha Niskin

You can use the familiar #! semantic to preprocess the JavaScript in your Golf apps. In any JavaScript file or component <script type="text/golf"> tag, simply prepend a #! line with the preprocessor you wish to have run on your JavaScript.

Examples

Here is a plugin/library using coffee-script (say plugins/mylib.js):

#!coffee -p
exports.isNumber: (x) -> typeof x == "number" || x instanceof Number
exports.sayhi: -> alert("hello there!")

And here is a component using coffee-script for its constructor:

<div>
  <style type="text/golf"></style>
<script type="text/golf">#!coffee -np
(a,b) ->
  @a: a
  @b: b
  @y: (x) -> @a*x + @b

  $("form").submit((event) =>
    x: $("input[name='x_in']").val()
    $(".xval").text(x)
    $(".yval").text(@y(parseFloat(x)))
    false
  ).submit()
</script>
<div>y(<span class="xval"/>) = <span class="yval"/></div>
  <form>
    x: <input type="text" name="x_in" value="0"/>
    <input type="submit"/>
  </form>
</div>

Note that coffee-script uses indentation to identify blocks, so the <script> tag is not indented.

blog comments powered by Disqus
Fork me on GitHub