Skip to content
Browse files

Fixed: inline templates with multiple apps.

  • Loading branch information...
1 parent 3d4b8ab commit d1ed1f63dac401914ddcc10b084a498641659d27 @mauricemach mauricemach committed Aug 3, 2011
Showing with 136 additions and 64 deletions.
  1. +2 −0 CHANGELOG.md
  2. +37 −3 docs/reference.md
  3. +44 −41 docs/zappa.html
  4. +2 −1 package.json
  5. +51 −19 src/zappa.coffee
View
2 CHANGELOG.md
@@ -1,6 +1,8 @@
**v0.2.0beta2** (2011-08-xx):
- Changed: external vars (`zappa {foo} ->`) overwrite all others.
+
+ - Fixed: inline templates with multiple apps.
**v0.2.0beta** (2011-08-02):
View
40 docs/reference.md
@@ -30,7 +30,7 @@ It will also lose access to its parent scope in the process, so any variables fr
zappa.app {foo}, ->
console.log foo # 'bar'
-Returns an object with properties `app` and `io`, which are the express and socket.io applications respectively.
+Returns an object with attributes `id` (uuid generated for this app), `app` (express server) and `io` (socket.io server).
### zappa.run
@@ -308,39 +308,73 @@ Node.js's global variables.
### @
+All request input data is merged here.
+
### response
+Directly from express.
+
### request
+Directly from express.
+
### next
+Directly from express.
+
### params
+Alias to `@`.
+
### send
+Shortcut to `response.send`.
+
### render
+Shortcut to `response.render`.
+
+Two additional features:
+
+ - All data from `@`/`params` is automatically passed on to the template as `params`.
+
+ - You can use `postrender`s: `render 'index', postrender: 'foo'`.
+
### redirect
+Shortcut to `response.redirect`.
+
## Sockets handlers scope
### @
+All input data is made accessible here.
+
### socket
+Directly from socket.io.
+
### id
+Shortcut to `socket.id`.
+
### params
+Alias to `@`.
+
### client
+You can put data pertaining to the client here. Alternative to `socket.set`.
+
### emit
+Shortcut to `socket.emit`.
+
### broadcast
-## View scope
+Shortcut to `socket.broadcast`.
-### @
+## View scope
## Client-side root scope
View
85 docs/zappa.html
@@ -5,6 +5,7 @@
<span class="nv">log = </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span>
<span class="nv">fs = </span><span class="nx">require</span> <span class="s1">&#39;fs&#39;</span>
<span class="nv">path = </span><span class="nx">require</span> <span class="s1">&#39;path&#39;</span>
+<span class="nv">uuid = </span><span class="nx">require</span> <span class="s1">&#39;node-uuid&#39;</span>
<span class="nv">express = </span><span class="nx">require</span> <span class="s1">&#39;express&#39;</span>
<span class="nv">socketio = </span><span class="nx">require</span> <span class="s1">&#39;socket.io&#39;</span>
<span class="nv">jsdom = </span><span class="nx">require</span> <span class="s1">&#39;jsdom&#39;</span>
@@ -34,58 +35,57 @@
<span class="nv">code = </span><span class="s2">&quot;#{coffeescript_helpers}return (#{code}).apply(context, args);&quot;</span>
<span class="k">for</span> <span class="nx">name</span> <span class="k">in</span> <span class="nx">locals_names</span>
<span class="nv">code = </span><span class="s2">&quot;var #{name} = locals.#{name};&quot;</span> <span class="o">+</span> <span class="nx">code</span>
- <span class="k">new</span> <span class="nb">Function</span><span class="p">(</span><span class="s1">&#39;context&#39;</span><span class="p">,</span> <span class="s1">&#39;locals&#39;</span><span class="p">,</span> <span class="s1">&#39;args&#39;</span><span class="p">,</span> <span class="nx">code</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">&#182;</a> </div> <p>The stringified zappa client.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">client = </span><span class="nx">require</span><span class="p">(</span><span class="s1">&#39;./client&#39;</span><span class="p">).</span><span class="nx">build</span><span class="p">(</span><span class="nx">zappa</span><span class="p">.</span><span class="nx">version</span><span class="p">,</span> <span class="nx">coffeescript_helpers</span><span class="p">,</span> <span class="nx">rewrite_function</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-5">&#182;</a> </div> <p>Takes in a function and builds express/socket.io apps based on the rules contained in it.
+ <span class="k">new</span> <span class="nb">Function</span><span class="p">(</span><span class="s1">&#39;context&#39;</span><span class="p">,</span> <span class="s1">&#39;locals&#39;</span><span class="p">,</span> <span class="s1">&#39;args&#39;</span><span class="p">,</span> <span class="nx">code</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">&#182;</a> </div> <p>The stringified zappa client.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">client = </span><span class="nx">require</span><span class="p">(</span><span class="s1">&#39;./client&#39;</span><span class="p">).</span><span class="nx">build</span><span class="p">(</span><span class="nx">zappa</span><span class="p">.</span><span class="nx">version</span><span class="p">,</span> <span class="nx">coffeescript_helpers</span><span class="p">,</span> <span class="nx">rewrite_function</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-5">&#182;</a> </div> <p>List of all apps created by this running copy of zappa.
+Keyed by the generated app uuid.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">apps = </span><span class="p">{}</span>
+ </pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-6">&#182;</a> </div> <p>Monkeypatch express to support lookup of inline templates. Such is life.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">express</span><span class="p">.</span><span class="nx">View</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">__defineGetter__</span> <span class="s1">&#39;exists&#39;</span><span class="p">,</span> <span class="o">-&gt;</span></pre></div> </td> </tr> <tr id="section-7"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-7">&#182;</a> </div> <p>Zappa's <code>render</code> sends the view name as appid|viewname.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="p">[</span><span class="nx">appid</span><span class="p">,</span> <span class="nx">view</span><span class="p">]</span> <span class="o">=</span> <span class="nx">@view</span><span class="p">.</span><span class="nx">split</span> <span class="s1">&#39;|&#39;</span></pre></div> </td> </tr> <tr id="section-8"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-8">&#182;</a> </div> <p>Try to find an inline template.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">return</span> <span class="kc">true</span> <span class="k">if</span> <span class="nx">apps</span><span class="p">[</span><span class="nx">appid</span><span class="p">]</span><span class="o">?</span><span class="p">.</span><span class="nx">views</span><span class="p">[</span><span class="nx">view</span><span class="p">]</span><span class="o">?</span>
+ <span class="k">return</span> <span class="kc">true</span> <span class="k">if</span> <span class="nx">apps</span><span class="p">[</span><span class="nx">appid</span><span class="p">]</span><span class="o">?</span><span class="p">.</span><span class="nx">views</span><span class="p">[</span><span class="nx">path</span><span class="p">.</span><span class="nx">basename</span><span class="p">(</span><span class="nx">view</span><span class="p">)]</span><span class="o">?</span></pre></div> </td> </tr> <tr id="section-9"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-9">&#182;</a> </div> <p>Inline template not found, take back the app id.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="vi">@path = </span><span class="nx">@path</span><span class="p">.</span><span class="nx">replace</span> <span class="nx">appid</span> <span class="o">+</span> <span class="s1">&#39;|&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span></pre></div> </td> </tr> <tr id="section-10"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-10">&#182;</a> </div> <p>Normal express behaviour.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">try</span>
+ <span class="nx">fs</span><span class="p">.</span><span class="nx">statSync</span><span class="p">(</span><span class="nx">@path</span><span class="p">)</span>
+ <span class="k">return</span> <span class="kc">true</span>
+ <span class="k">catch</span> <span class="nx">err</span>
+ <span class="k">return</span> <span class="kc">false</span>
+
+<span class="nx">express</span><span class="p">.</span><span class="nx">View</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">__defineGetter__</span> <span class="s1">&#39;contents&#39;</span><span class="p">,</span> <span class="o">-&gt;</span></pre></div> </td> </tr> <tr id="section-11"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-11">&#182;</a> </div> <p>Zappa's <code>render</code> sends the view name as appid|viewname.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="p">[</span><span class="nx">appid</span><span class="p">,</span> <span class="nx">view</span><span class="p">]</span> <span class="o">=</span> <span class="nx">@view</span><span class="p">.</span><span class="nx">split</span> <span class="s1">&#39;|&#39;</span></pre></div> </td> </tr> <tr id="section-12"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-12">&#182;</a> </div> <p>Try to find an inline template.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">return</span> <span class="nx">apps</span><span class="p">[</span><span class="nx">appid</span><span class="p">]</span><span class="o">?</span><span class="p">.</span><span class="nx">views</span><span class="p">[</span><span class="nx">view</span><span class="p">]</span> <span class="k">if</span> <span class="nx">apps</span><span class="p">[</span><span class="nx">appid</span><span class="p">]</span><span class="o">?</span><span class="p">.</span><span class="nx">views</span><span class="p">[</span><span class="nx">view</span><span class="p">]</span><span class="o">?</span>
+ <span class="k">return</span> <span class="nx">apps</span><span class="p">[</span><span class="nx">appid</span><span class="p">]</span><span class="o">?</span><span class="p">.</span><span class="nx">views</span><span class="p">[</span><span class="nx">path</span><span class="p">.</span><span class="nx">basename</span><span class="p">(</span><span class="nx">view</span><span class="p">)]</span> <span class="k">if</span> <span class="nx">apps</span><span class="p">[</span><span class="nx">appid</span><span class="p">]</span><span class="o">?</span><span class="p">.</span><span class="nx">views</span><span class="p">[</span><span class="nx">path</span><span class="p">.</span><span class="nx">basename</span><span class="p">(</span><span class="nx">view</span><span class="p">)]</span><span class="o">?</span></pre></div> </td> </tr> <tr id="section-13"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-13">&#182;</a> </div> <p>Inline template not found, take back the app id.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="vi">@path = </span><span class="nx">@path</span><span class="p">.</span><span class="nx">replace</span> <span class="nx">appid</span> <span class="o">+</span> <span class="s1">&#39;|&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span></pre></div> </td> </tr> <tr id="section-14"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-14">&#182;</a> </div> <p>Normal express behaviour.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">fs</span><span class="p">.</span><span class="nx">readFileSync</span> <span class="nx">@path</span><span class="p">,</span> <span class="s1">&#39;utf8&#39;</span></pre></div> </td> </tr> <tr id="section-15"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-15">&#182;</a> </div> <p>Takes in a function and builds express/socket.io apps based on the rules contained in it.
The optional data object allows passing variables from the outside to the root scope.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">zappa.app = </span><span class="o">-&gt;</span>
+ <span class="nv">id = </span><span class="nx">uuid</span><span class="p">()</span>
+ <span class="nx">apps</span><span class="p">[</span><span class="nx">id</span><span class="p">]</span> <span class="o">=</span> <span class="p">{}</span>
+
<span class="nv">data = </span><span class="kc">null</span>
<span class="nv">root_function = </span><span class="kc">null</span>
<span class="k">for</span> <span class="nx">a</span> <span class="k">in</span> <span class="nx">arguments</span>
<span class="k">switch</span> <span class="k">typeof</span> <span class="nx">a</span>
<span class="k">when</span> <span class="s1">&#39;function&#39;</span> <span class="k">then</span> <span class="nv">root_function = </span><span class="nx">a</span>
- <span class="k">when</span> <span class="s1">&#39;object&#39;</span> <span class="k">then</span> <span class="nv">data = </span><span class="nx">a</span></pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-6">&#182;</a> </div> <p>Names of local variables that we have to know beforehand, to use with <code>rewrite_function</code>.
-Helpers and defs will be known after we execute the user-provided <code>root_function</code>.</p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-7"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-7">&#182;</a> </div> <p>The last four (<code>require</code>, <code>module</code>, <code>__filename</code> and <code>__dirname</code>) are not actually real globals,
+ <span class="k">when</span> <span class="s1">&#39;object&#39;</span> <span class="k">then</span> <span class="nv">data = </span><span class="nx">a</span></pre></div> </td> </tr> <tr id="section-16"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-16">&#182;</a> </div> <p>Names of local variables that we have to know beforehand, to use with <code>rewrite_function</code>.
+Helpers and defs will be known after we execute the user-provided <code>root_function</code>.</p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-17"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-17">&#182;</a> </div> <p>The last four (<code>require</code>, <code>module</code>, <code>__filename</code> and <code>__dirname</code>) are not actually real globals,
but locals to each module.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">globals_names = </span><span class="p">[</span><span class="s1">&#39;global&#39;</span><span class="p">,</span> <span class="s1">&#39;process&#39;</span><span class="p">,</span> <span class="s1">&#39;console&#39;</span><span class="p">,</span> <span class="s1">&#39;setTimeout&#39;</span><span class="p">,</span> <span class="s1">&#39;clearTimeout&#39;</span><span class="p">,</span> <span class="s1">&#39;setInterval&#39;</span><span class="p">,</span> <span class="s1">&#39;clearInterval&#39;</span><span class="p">,</span>
- <span class="s1">&#39;require&#39;</span><span class="p">,</span> <span class="s1">&#39;module&#39;</span><span class="p">,</span> <span class="s1">&#39;__filename&#39;</span><span class="p">,</span> <span class="s1">&#39;__dirname&#39;</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-8"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-8">&#182;</a> </div> <p>TODO: route?, error</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">root_names = </span><span class="p">[</span><span class="s1">&#39;zappa&#39;</span><span class="p">,</span> <span class="s1">&#39;express&#39;</span><span class="p">,</span> <span class="s1">&#39;app&#39;</span><span class="p">,</span> <span class="s1">&#39;io&#39;</span><span class="p">,</span> <span class="s1">&#39;requiring&#39;</span><span class="p">,</span> <span class="s1">&#39;get&#39;</span><span class="p">,</span> <span class="s1">&#39;post&#39;</span><span class="p">,</span> <span class="s1">&#39;put&#39;</span><span class="p">,</span> <span class="s1">&#39;del&#39;</span><span class="p">,</span> <span class="s1">&#39;at&#39;</span><span class="p">,</span>
+ <span class="s1">&#39;require&#39;</span><span class="p">,</span> <span class="s1">&#39;module&#39;</span><span class="p">,</span> <span class="s1">&#39;__filename&#39;</span><span class="p">,</span> <span class="s1">&#39;__dirname&#39;</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-18"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-18">&#182;</a> </div> <p>TODO: route?, error</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">root_names = </span><span class="p">[</span><span class="s1">&#39;zappa&#39;</span><span class="p">,</span> <span class="s1">&#39;express&#39;</span><span class="p">,</span> <span class="s1">&#39;app&#39;</span><span class="p">,</span> <span class="s1">&#39;io&#39;</span><span class="p">,</span> <span class="s1">&#39;requiring&#39;</span><span class="p">,</span> <span class="s1">&#39;get&#39;</span><span class="p">,</span> <span class="s1">&#39;post&#39;</span><span class="p">,</span> <span class="s1">&#39;put&#39;</span><span class="p">,</span> <span class="s1">&#39;del&#39;</span><span class="p">,</span> <span class="s1">&#39;at&#39;</span><span class="p">,</span>
<span class="s1">&#39;helper&#39;</span><span class="p">,</span> <span class="s1">&#39;def&#39;</span><span class="p">,</span> <span class="s1">&#39;view&#39;</span><span class="p">,</span> <span class="s1">&#39;set&#39;</span><span class="p">,</span> <span class="s1">&#39;use&#39;</span><span class="p">,</span> <span class="s1">&#39;configure&#39;</span><span class="p">,</span> <span class="s1">&#39;include&#39;</span><span class="p">,</span> <span class="s1">&#39;shared&#39;</span><span class="p">,</span> <span class="s1">&#39;client&#39;</span><span class="p">,</span> <span class="s1">&#39;coffee&#39;</span><span class="p">,</span> <span class="s1">&#39;js&#39;</span><span class="p">,</span> <span class="s1">&#39;css&#39;</span><span class="p">,</span>
- <span class="s1">&#39;enable&#39;</span><span class="p">,</span> <span class="s1">&#39;disable&#39;</span><span class="p">,</span> <span class="s1">&#39;settings&#39;</span><span class="p">,</span> <span class="s1">&#39;postrender&#39;</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-9"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-9">&#182;</a> </div> <p>TODO: session, cookies, app data, clients list</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">http_names = </span><span class="p">[</span><span class="s1">&#39;app&#39;</span><span class="p">,</span> <span class="s1">&#39;settings&#39;</span><span class="p">,</span> <span class="s1">&#39;response&#39;</span><span class="p">,</span> <span class="s1">&#39;request&#39;</span><span class="p">,</span> <span class="s1">&#39;next&#39;</span><span class="p">,</span> <span class="s1">&#39;params&#39;</span><span class="p">,</span> <span class="s1">&#39;send&#39;</span><span class="p">,</span> <span class="s1">&#39;render&#39;</span><span class="p">,</span> <span class="s1">&#39;redirect&#39;</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-10"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-10">&#182;</a> </div> <p>TODO: app data, clients list, join</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">ws_names = </span><span class="p">[</span><span class="s1">&#39;app&#39;</span><span class="p">,</span> <span class="s1">&#39;io&#39;</span><span class="p">,</span> <span class="s1">&#39;settings&#39;</span><span class="p">,</span> <span class="s1">&#39;socket&#39;</span><span class="p">,</span> <span class="s1">&#39;id&#39;</span><span class="p">,</span> <span class="s1">&#39;params&#39;</span><span class="p">,</span> <span class="s1">&#39;client&#39;</span><span class="p">,</span> <span class="s1">&#39;emit&#39;</span><span class="p">,</span> <span class="s1">&#39;broadcast&#39;</span><span class="p">]</span>
+ <span class="s1">&#39;enable&#39;</span><span class="p">,</span> <span class="s1">&#39;disable&#39;</span><span class="p">,</span> <span class="s1">&#39;settings&#39;</span><span class="p">,</span> <span class="s1">&#39;postrender&#39;</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-19"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-19">&#182;</a> </div> <p>TODO: session, cookies, app data, clients list</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">http_names = </span><span class="p">[</span><span class="s1">&#39;app&#39;</span><span class="p">,</span> <span class="s1">&#39;settings&#39;</span><span class="p">,</span> <span class="s1">&#39;response&#39;</span><span class="p">,</span> <span class="s1">&#39;request&#39;</span><span class="p">,</span> <span class="s1">&#39;next&#39;</span><span class="p">,</span> <span class="s1">&#39;params&#39;</span><span class="p">,</span> <span class="s1">&#39;send&#39;</span><span class="p">,</span> <span class="s1">&#39;render&#39;</span><span class="p">,</span> <span class="s1">&#39;redirect&#39;</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-20"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-20">&#182;</a> </div> <p>TODO: app data, clients list, join</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">ws_names = </span><span class="p">[</span><span class="s1">&#39;app&#39;</span><span class="p">,</span> <span class="s1">&#39;io&#39;</span><span class="p">,</span> <span class="s1">&#39;settings&#39;</span><span class="p">,</span> <span class="s1">&#39;socket&#39;</span><span class="p">,</span> <span class="s1">&#39;id&#39;</span><span class="p">,</span> <span class="s1">&#39;params&#39;</span><span class="p">,</span> <span class="s1">&#39;client&#39;</span><span class="p">,</span> <span class="s1">&#39;emit&#39;</span><span class="p">,</span> <span class="s1">&#39;broadcast&#39;</span><span class="p">]</span>
<span class="nv">postrender_names = </span><span class="p">[</span><span class="s1">&#39;window&#39;</span><span class="p">,</span> <span class="s1">&#39;$&#39;</span><span class="p">]</span>
<span class="nv">externals_names = </span><span class="p">[]</span>
<span class="nx">externals_names</span><span class="p">.</span><span class="nx">push</span> <span class="nx">k</span> <span class="k">for</span> <span class="nx">k</span><span class="p">,</span> <span class="nx">v</span> <span class="k">of</span> <span class="nx">data</span>
<span class="nv">helpers_names = </span><span class="p">[]</span>
- <span class="nv">defs_names = </span><span class="p">[]</span></pre></div> </td> </tr> <tr id="section-11"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-11">&#182;</a> </div> <p>Storage for the user-provided handlers.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">routes = </span><span class="p">[]</span>
+ <span class="nv">defs_names = </span><span class="p">[]</span></pre></div> </td> </tr> <tr id="section-21"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-21">&#182;</a> </div> <p>Storage for the user-provided handlers.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">routes = </span><span class="p">[]</span>
<span class="nv">ws_handlers = </span><span class="p">{}</span>
<span class="nv">helpers = </span><span class="p">{}</span>
- <span class="nv">defs = </span><span class="p">{}</span>
- <span class="nv">views = </span><span class="p">{}</span>
+ <span class="nv">defs = </span><span class="p">{}</span></pre></div> </td> </tr> <tr id="section-22"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-22">&#182;</a> </div> <p>Keep inline views at the module level and namespaced by app id
+so that the monkeypatched express can look them up.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">apps</span><span class="p">[</span><span class="nx">id</span><span class="p">].</span><span class="nv">views = </span><span class="p">{}</span>
<span class="nv">postrenders = </span><span class="p">{}</span>
- </pre></div> </td> </tr> <tr id="section-12"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-12">&#182;</a> </div> <p>Monkeypatch express to support inline templates. Such is life.
-TODO: this doesn't work for multiple apps.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">express</span><span class="p">.</span><span class="nx">View</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">__defineGetter__</span> <span class="s1">&#39;exists&#39;</span><span class="p">,</span> <span class="o">-&gt;</span>
- <span class="k">return</span> <span class="kc">true</span> <span class="k">if</span> <span class="nx">views</span><span class="p">[</span><span class="nx">@view</span><span class="p">]</span><span class="o">?</span>
- <span class="k">return</span> <span class="kc">true</span> <span class="k">if</span> <span class="nx">views</span><span class="p">[</span><span class="nx">path</span><span class="p">.</span><span class="nx">basename</span><span class="p">(</span><span class="nx">@view</span><span class="p">)]</span><span class="o">?</span>
- <span class="k">try</span>
- <span class="nx">fs</span><span class="p">.</span><span class="nx">statSync</span><span class="p">(</span><span class="nx">@path</span><span class="p">)</span>
- <span class="k">return</span> <span class="kc">true</span>
- <span class="k">catch</span> <span class="nx">err</span>
- <span class="k">return</span> <span class="kc">false</span>
-
- <span class="nx">express</span><span class="p">.</span><span class="nx">View</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">__defineGetter__</span> <span class="s1">&#39;contents&#39;</span><span class="p">,</span> <span class="o">-&gt;</span>
- <span class="k">return</span> <span class="nx">views</span><span class="p">[</span><span class="nx">@view</span><span class="p">]</span> <span class="k">if</span> <span class="nx">views</span><span class="p">[</span><span class="nx">@view</span><span class="p">]</span><span class="o">?</span>
- <span class="k">return</span> <span class="nx">views</span><span class="p">[</span><span class="nx">path</span><span class="p">.</span><span class="nx">basename</span><span class="p">(</span><span class="nx">@view</span><span class="p">)]</span> <span class="k">if</span> <span class="nx">views</span><span class="p">[</span><span class="nx">path</span><span class="p">.</span><span class="nx">basename</span><span class="p">(</span><span class="nx">@view</span><span class="p">)]</span><span class="o">?</span>
- <span class="nx">fs</span><span class="p">.</span><span class="nx">readFileSync</span> <span class="nx">@path</span><span class="p">,</span> <span class="s1">&#39;utf8&#39;</span>
<span class="nv">app = </span><span class="nx">express</span><span class="p">.</span><span class="nx">createServer</span><span class="p">()</span>
- <span class="nv">io = </span><span class="nx">socketio</span><span class="p">.</span><span class="nx">listen</span><span class="p">(</span><span class="nx">app</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-13"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-13">&#182;</a> </div> <p>Zappa's default settings.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">app</span><span class="p">.</span><span class="nx">set</span> <span class="s1">&#39;view engine&#39;</span><span class="p">,</span> <span class="s1">&#39;coffee&#39;</span>
+ <span class="nv">io = </span><span class="nx">socketio</span><span class="p">.</span><span class="nx">listen</span><span class="p">(</span><span class="nx">app</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-23"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-23">&#182;</a> </div> <p>Zappa's default settings.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">app</span><span class="p">.</span><span class="nx">set</span> <span class="s1">&#39;view engine&#39;</span><span class="p">,</span> <span class="s1">&#39;coffee&#39;</span>
<span class="nx">app</span><span class="p">.</span><span class="nx">register</span> <span class="s1">&#39;.coffee&#39;</span><span class="p">,</span> <span class="nx">zappa</span><span class="p">.</span><span class="nx">adapter</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;coffeekup&#39;</span><span class="p">).</span><span class="nx">adapters</span><span class="p">.</span><span class="nx">express</span><span class="p">,</span>
- <span class="nv">blacklist: </span><span class="p">[</span><span class="s1">&#39;format&#39;</span><span class="p">,</span> <span class="s1">&#39;autoescape&#39;</span><span class="p">,</span> <span class="s1">&#39;locals&#39;</span><span class="p">,</span> <span class="s1">&#39;hardcode&#39;</span><span class="p">,</span> <span class="s1">&#39;cache&#39;</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-14"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-14">&#182;</a> </div> <p>Builds the applications's root scope.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">root_context = </span><span class="p">{}</span>
+ <span class="nv">blacklist: </span><span class="p">[</span><span class="s1">&#39;format&#39;</span><span class="p">,</span> <span class="s1">&#39;autoescape&#39;</span><span class="p">,</span> <span class="s1">&#39;locals&#39;</span><span class="p">,</span> <span class="s1">&#39;hardcode&#39;</span><span class="p">,</span> <span class="s1">&#39;cache&#39;</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-24"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-24">&#182;</a> </div> <p>Builds the applications's root scope.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">root_context = </span><span class="p">{}</span>
<span class="nv">root_locals = </span><span class="p">{</span><span class="nx">zappa</span><span class="p">,</span> <span class="nx">express</span><span class="p">,</span> <span class="nx">app</span><span class="p">,</span> <span class="nx">io</span><span class="p">}</span>
<span class="nx">root_locals</span><span class="p">[</span><span class="nx">g</span><span class="p">]</span> <span class="o">=</span> <span class="nb">eval</span><span class="p">(</span><span class="nx">g</span><span class="p">)</span> <span class="k">for</span> <span class="nx">g</span> <span class="k">in</span> <span class="nx">globals_names</span>
- </pre></div> </td> </tr> <tr id="section-15"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-15">&#182;</a> </div> <p>These "globals" are actually local to each module, so we get our values
+ </pre></div> </td> </tr> <tr id="section-25"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-25">&#182;</a> </div> <p>These "globals" are actually local to each module, so we get our values
from our parent module.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">root_locals.module = </span><span class="nx">module</span><span class="p">.</span><span class="nx">parent</span>
<span class="nv">root_locals.__filename = </span><span class="nx">module</span><span class="p">.</span><span class="nx">parent</span><span class="p">.</span><span class="nx">filename</span>
- <span class="nv">root_locals.__dirname = </span><span class="nx">path</span><span class="p">.</span><span class="nx">dirname</span><span class="p">(</span><span class="nx">module</span><span class="p">.</span><span class="nx">parent</span><span class="p">.</span><span class="nx">filename</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-16"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-16">&#182;</a> </div> <p>TODO: Find out how to pass the correct <code>require</code> (the module's, not zappa's) to the app.
+ <span class="nv">root_locals.__dirname = </span><span class="nx">path</span><span class="p">.</span><span class="nx">dirname</span><span class="p">(</span><span class="nx">module</span><span class="p">.</span><span class="nx">parent</span><span class="p">.</span><span class="nx">filename</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-26"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-26">&#182;</a> </div> <p>TODO: Find out how to pass the correct <code>require</code> (the module's, not zappa's) to the app.
root_locals.require = ???
Meanwhile, adding the app's root dir to the front of the lookup stack.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">require</span><span class="p">.</span><span class="nx">paths</span><span class="p">.</span><span class="nx">unshift</span> <span class="nx">root_locals</span><span class="p">.</span><span class="nx">__dirname</span>
@@ -139,7 +139,7 @@
<span class="nv">root_locals.view = </span><span class="nf">(obj) -&gt;</span>
<span class="k">for</span> <span class="nx">k</span><span class="p">,</span> <span class="nx">v</span> <span class="k">of</span> <span class="nx">obj</span>
- <span class="nx">views</span><span class="p">[</span><span class="nx">k</span><span class="p">]</span> <span class="o">=</span> <span class="nx">v</span>
+ <span class="nx">apps</span><span class="p">[</span><span class="nx">id</span><span class="p">].</span><span class="nx">views</span><span class="p">[</span><span class="nx">k</span><span class="p">]</span> <span class="o">=</span> <span class="nx">v</span>
<span class="nv">root_locals.set = </span><span class="nf">(obj) -&gt;</span>
<span class="k">for</span> <span class="nx">k</span><span class="p">,</span> <span class="nx">v</span> <span class="k">of</span> <span class="nx">obj</span>
@@ -197,12 +197,12 @@
<span class="nx">include_locals</span><span class="p">[</span><span class="nx">k</span><span class="p">]</span> <span class="o">=</span> <span class="nx">v</span> <span class="k">for</span> <span class="nx">k</span><span class="p">,</span> <span class="nx">v</span> <span class="k">of</span> <span class="nx">root_locals</span>
<span class="nv">include_module = </span><span class="nx">require</span><span class="p">.</span><span class="nx">cache</span><span class="p">[</span><span class="nx">require</span><span class="p">.</span><span class="nx">resolve</span><span class="p">(</span><span class="nx">name</span><span class="p">)]</span>
- </pre></div> </td> </tr> <tr id="section-17"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-17">&#182;</a> </div> <p>These "globals" are actually local to each module, so we get our values
+ </pre></div> </td> </tr> <tr id="section-27"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-27">&#182;</a> </div> <p>These "globals" are actually local to each module, so we get our values
from the required module.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">include_locals.module = </span><span class="nx">include_module</span>
<span class="nv">include_locals.__filename = </span><span class="nx">include_module</span><span class="p">.</span><span class="nx">filename</span>
- <span class="nv">include_locals.__dirname = </span><span class="nx">path</span><span class="p">.</span><span class="nx">dirname</span><span class="p">(</span><span class="nx">include_module</span><span class="p">.</span><span class="nx">filename</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-18"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-18">&#182;</a> </div> <p>TODO: Find out how to pass the correct <code>require</code> (the module's, not zappa's) to the include.
-include_locals.require = ???</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">rewritten_sub</span><span class="p">(</span><span class="nx">root_context</span><span class="p">,</span> <span class="nx">include_locals</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-19"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-19">&#182;</a> </div> <p>Variables passed through the object parameter.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">root_locals</span><span class="p">[</span><span class="nx">k</span><span class="p">]</span> <span class="o">=</span> <span class="nx">v</span> <span class="k">for</span> <span class="nx">k</span><span class="p">,</span> <span class="nx">v</span> <span class="k">of</span> <span class="nx">data</span></pre></div> </td> </tr> <tr id="section-20"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-20">&#182;</a> </div> <p>Executes the (rewriten) end-user function and learns how the app should be structured.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">rewritten_root = </span><span class="nx">rewrite_function</span><span class="p">(</span><span class="nx">root_function</span><span class="p">,</span> <span class="nx">root_names</span><span class="p">.</span><span class="nx">concat</span><span class="p">(</span><span class="nx">globals_names</span><span class="p">).</span><span class="nx">concat</span><span class="p">(</span><span class="nx">externals_names</span><span class="p">))</span>
- <span class="nx">rewritten_root</span><span class="p">(</span><span class="nx">root_context</span><span class="p">,</span> <span class="nx">root_locals</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-21"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-21">&#182;</a> </div> <p>Implements the application according to the specification.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">for</span> <span class="nx">k</span><span class="p">,</span> <span class="nx">v</span> <span class="k">of</span> <span class="nx">helpers</span>
+ <span class="nv">include_locals.__dirname = </span><span class="nx">path</span><span class="p">.</span><span class="nx">dirname</span><span class="p">(</span><span class="nx">include_module</span><span class="p">.</span><span class="nx">filename</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-28"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-28">&#182;</a> </div> <p>TODO: Find out how to pass the correct <code>require</code> (the module's, not zappa's) to the include.
+include_locals.require = ???</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">rewritten_sub</span><span class="p">(</span><span class="nx">root_context</span><span class="p">,</span> <span class="nx">include_locals</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-29"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-29">&#182;</a> </div> <p>Variables passed through the object parameter.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">root_locals</span><span class="p">[</span><span class="nx">k</span><span class="p">]</span> <span class="o">=</span> <span class="nx">v</span> <span class="k">for</span> <span class="nx">k</span><span class="p">,</span> <span class="nx">v</span> <span class="k">of</span> <span class="nx">data</span></pre></div> </td> </tr> <tr id="section-30"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-30">&#182;</a> </div> <p>Executes the (rewriten) end-user function and learns how the app should be structured.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">rewritten_root = </span><span class="nx">rewrite_function</span><span class="p">(</span><span class="nx">root_function</span><span class="p">,</span> <span class="nx">root_names</span><span class="p">.</span><span class="nx">concat</span><span class="p">(</span><span class="nx">globals_names</span><span class="p">).</span><span class="nx">concat</span><span class="p">(</span><span class="nx">externals_names</span><span class="p">))</span>
+ <span class="nx">rewritten_root</span><span class="p">(</span><span class="nx">root_context</span><span class="p">,</span> <span class="nx">root_locals</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-31"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-31">&#182;</a> </div> <p>Implements the application according to the specification.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">for</span> <span class="nx">k</span><span class="p">,</span> <span class="nx">v</span> <span class="k">of</span> <span class="nx">helpers</span>
<span class="nx">helpers</span><span class="p">[</span><span class="nx">k</span><span class="p">]</span> <span class="o">=</span> <span class="nx">rewrite_function</span><span class="p">(</span><span class="nx">v</span><span class="p">,</span> <span class="nx">http_names</span><span class="p">.</span><span class="nx">concat</span><span class="p">(</span><span class="nx">helpers_names</span><span class="p">).</span><span class="nx">concat</span><span class="p">(</span><span class="nx">defs_names</span><span class="p">).</span><span class="nx">concat</span><span class="p">(</span><span class="nx">globals_names</span><span class="p">))</span>
<span class="k">for</span> <span class="nx">k</span><span class="p">,</span> <span class="nx">v</span> <span class="k">of</span> <span class="nx">ws_handlers</span>
@@ -241,7 +241,7 @@
<span class="nx">link</span> <span class="nv">rel: </span><span class="s1">&#39;stylesheet&#39;</span><span class="p">,</span> <span class="nv">href: </span><span class="nx">s</span> <span class="o">+</span> <span class="s1">&#39;.css&#39;</span>
<span class="nx">link</span><span class="p">(</span><span class="nv">rel: </span><span class="s1">&#39;stylesheet&#39;</span><span class="p">,</span> <span class="nv">href: </span><span class="nx">@stylesheet</span> <span class="o">+</span> <span class="s1">&#39;.css&#39;</span><span class="p">)</span> <span class="k">if</span> <span class="nx">@stylesheet</span>
<span class="nx">style</span> <span class="nx">@style</span> <span class="k">if</span> <span class="nx">@style</span>
- <span class="nx">body</span> <span class="nx">@body</span></pre></div> </td> </tr> <tr id="section-22"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-22">&#182;</a> </div> <p>Implements the http server with express.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">for</span> <span class="nx">r</span> <span class="k">in</span> <span class="nx">routes</span>
+ <span class="nx">body</span> <span class="nx">@body</span></pre></div> </td> </tr> <tr id="section-32"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-32">&#182;</a> </div> <p>Implements the http server with express.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">for</span> <span class="nx">r</span> <span class="k">in</span> <span class="nx">routes</span>
<span class="nx">do</span> <span class="nf">(r) -&gt;</span>
<span class="k">if</span> <span class="k">typeof</span> <span class="nx">r</span><span class="p">.</span><span class="nx">handler</span> <span class="o">is</span> <span class="s1">&#39;string&#39;</span>
<span class="nx">app</span><span class="p">[</span><span class="nx">r</span><span class="p">.</span><span class="nx">verb</span><span class="p">]</span> <span class="nx">r</span><span class="p">.</span><span class="nx">path</span><span class="p">,</span> <span class="nf">(req, res) -&gt;</span>
@@ -253,7 +253,7 @@
<span class="nv">context = </span><span class="kc">null</span>
<span class="nv">locals = </span><span class="p">{</span><span class="nx">app</span><span class="p">,</span> <span class="nv">settings: </span><span class="nx">app</span><span class="p">.</span><span class="nx">settings</span><span class="p">}</span>
- </pre></div> </td> </tr> <tr id="section-23"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-23">&#182;</a> </div> <p>TODO: fix pseudo-globals here too.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">locals</span><span class="p">[</span><span class="nx">g</span><span class="p">]</span> <span class="o">=</span> <span class="nb">eval</span><span class="p">(</span><span class="nx">g</span><span class="p">)</span> <span class="k">for</span> <span class="nx">g</span> <span class="k">in</span> <span class="nx">globals_names</span>
+ </pre></div> </td> </tr> <tr id="section-33"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-33">&#182;</a> </div> <p>TODO: fix pseudo-globals here too.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">locals</span><span class="p">[</span><span class="nx">g</span><span class="p">]</span> <span class="o">=</span> <span class="nb">eval</span><span class="p">(</span><span class="nx">g</span><span class="p">)</span> <span class="k">for</span> <span class="nx">g</span> <span class="k">in</span> <span class="nx">globals_names</span>
<span class="k">for</span> <span class="nx">name</span><span class="p">,</span> <span class="nx">def</span> <span class="k">of</span> <span class="nx">defs</span>
<span class="nx">locals</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span> <span class="o">=</span> <span class="nx">def</span>
@@ -276,11 +276,14 @@
<span class="nv">locals.send = </span><span class="o">-&gt;</span> <span class="nx">res</span><span class="p">.</span><span class="nx">send</span><span class="p">.</span><span class="nx">apply</span> <span class="nx">res</span><span class="p">,</span> <span class="nx">arguments</span>
<span class="nv">locals.redirect = </span><span class="o">-&gt;</span> <span class="nx">res</span><span class="p">.</span><span class="nx">redirect</span><span class="p">.</span><span class="nx">apply</span> <span class="nx">res</span><span class="p">,</span> <span class="nx">arguments</span>
- <span class="nv">locals.render = </span><span class="nf">(args...) -&gt;</span></pre></div> </td> </tr> <tr id="section-24"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-24">&#182;</a> </div> <p>Make sure the second arg is an object.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">args</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">?=</span> <span class="p">{}</span>
+ <span class="nv">locals.render = </span><span class="nf">(args...) -&gt;</span></pre></div> </td> </tr> <tr id="section-34"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-34">&#182;</a> </div> <p>Adds the app id to the view name so that the monkeypatched
+express.View.exists and express.View.contents can lookup
+this app's inline templates.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">args</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="nx">id</span> <span class="o">+</span> <span class="s1">&#39;|&#39;</span> <span class="o">+</span> <span class="nx">args</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
+ </pre></div> </td> </tr> <tr id="section-35"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-35">&#182;</a> </div> <p>Make sure the second arg is an object.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">args</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">?=</span> <span class="p">{}</span>
<span class="nx">args</span><span class="p">.</span><span class="nx">splice</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="p">{}</span> <span class="k">if</span> <span class="k">typeof</span> <span class="nx">args</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">is</span> <span class="s1">&#39;function&#39;</span>
- </pre></div> </td> </tr> <tr id="section-25"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-25">&#182;</a> </div> <p>Send request input vars to template as <code>params</code>.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">args</span><span class="p">[</span><span class="mi">1</span><span class="p">].</span><span class="nx">params</span> <span class="o">?=</span> <span class="nx">locals</span><span class="p">.</span><span class="nx">params</span>
+ </pre></div> </td> </tr> <tr id="section-36"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-36">&#182;</a> </div> <p>Send request input vars to template as <code>params</code>.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">args</span><span class="p">[</span><span class="mi">1</span><span class="p">].</span><span class="nx">params</span> <span class="o">?=</span> <span class="nx">locals</span><span class="p">.</span><span class="nx">params</span>
- <span class="k">if</span> <span class="nx">args</span><span class="p">[</span><span class="mi">1</span><span class="p">].</span><span class="nx">postrender</span><span class="o">?</span></pre></div> </td> </tr> <tr id="section-26"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-26">&#182;</a> </div> <p>Apply postrender before sending response.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">res</span><span class="p">.</span><span class="nx">render</span> <span class="nx">args</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="nx">args</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="nf">(err, str) -&gt;</span>
+ <span class="k">if</span> <span class="nx">args</span><span class="p">[</span><span class="mi">1</span><span class="p">].</span><span class="nx">postrender</span><span class="o">?</span></pre></div> </td> </tr> <tr id="section-37"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-37">&#182;</a> </div> <p>Apply postrender before sending response.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">res</span><span class="p">.</span><span class="nx">render</span> <span class="nx">args</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="nx">args</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="nf">(err, str) -&gt;</span>
<span class="nx">jsdom</span><span class="p">.</span><span class="nx">env</span> <span class="nv">html: </span><span class="nx">str</span><span class="p">,</span> <span class="nv">src: </span><span class="p">[</span><span class="nx">jquery</span><span class="p">],</span> <span class="nv">done: </span><span class="nf">(err, window) -&gt;</span>
<span class="nv">locals.window = </span><span class="nb">window</span>
<span class="nx">locals</span><span class="p">.</span><span class="nv">$ = </span><span class="nb">window</span><span class="p">.</span><span class="nx">$</span>
@@ -289,13 +292,13 @@
<span class="nv">doctype = </span><span class="p">(</span><span class="nb">window</span><span class="p">.</span><span class="nb">document</span><span class="p">.</span><span class="nx">doctype</span> <span class="o">or</span> <span class="s1">&#39;&#39;</span><span class="p">)</span> <span class="o">+</span> <span class="s2">&quot;\n&quot;</span>
<span class="nx">res</span><span class="p">.</span><span class="nx">send</span> <span class="nx">doctype</span> <span class="o">+</span> <span class="nb">window</span><span class="p">.</span><span class="nb">document</span><span class="p">.</span><span class="nx">documentElement</span><span class="p">.</span><span class="nx">outerHTML</span>
- <span class="k">else</span></pre></div> </td> </tr> <tr id="section-27"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-27">&#182;</a> </div> <p>Just forward params to express.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">res</span><span class="p">.</span><span class="nx">render</span><span class="p">.</span><span class="nx">apply</span> <span class="nx">res</span><span class="p">,</span> <span class="nx">args</span>
+ <span class="k">else</span></pre></div> </td> </tr> <tr id="section-38"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-38">&#182;</a> </div> <p>Just forward params to express.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">res</span><span class="p">.</span><span class="nx">render</span><span class="p">.</span><span class="nx">apply</span> <span class="nx">res</span><span class="p">,</span> <span class="nx">args</span>
<span class="nv">result = </span><span class="nx">rewritten_handler</span><span class="p">(</span><span class="nx">context</span><span class="p">,</span> <span class="nx">locals</span><span class="p">)</span>
<span class="nx">res</span><span class="p">.</span><span class="nx">contentType</span><span class="p">(</span><span class="nx">r</span><span class="p">.</span><span class="nx">contentType</span><span class="p">)</span> <span class="k">if</span> <span class="nx">r</span><span class="p">.</span><span class="nx">contentType</span><span class="o">?</span>
<span class="k">if</span> <span class="k">typeof</span> <span class="nx">result</span> <span class="o">is</span> <span class="s1">&#39;string&#39;</span> <span class="k">then</span> <span class="nx">res</span><span class="p">.</span><span class="nx">send</span> <span class="nx">result</span>
- <span class="k">else</span> <span class="k">return</span> <span class="nx">result</span></pre></div> </td> </tr> <tr id="section-28"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-28">&#182;</a> </div> <p>Implements the websockets server with socket.io.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">io</span><span class="p">.</span><span class="nx">sockets</span><span class="p">.</span><span class="kc">on</span> <span class="s1">&#39;connection&#39;</span><span class="p">,</span> <span class="nf">(socket) -&gt;</span>
+ <span class="k">else</span> <span class="k">return</span> <span class="nx">result</span></pre></div> </td> </tr> <tr id="section-39"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-39">&#182;</a> </div> <p>Implements the websockets server with socket.io.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">io</span><span class="p">.</span><span class="nx">sockets</span><span class="p">.</span><span class="kc">on</span> <span class="s1">&#39;connection&#39;</span><span class="p">,</span> <span class="nf">(socket) -&gt;</span>
<span class="nv">context = </span><span class="p">{}</span>
<span class="nv">locals =</span>
<span class="nv">app: </span><span class="nx">app</span>
@@ -305,7 +308,7 @@
<span class="nv">id: </span><span class="nx">socket</span><span class="p">.</span><span class="nx">id</span>
<span class="nv">client: </span><span class="p">{}</span>
<span class="nv">emit: </span><span class="o">-&gt;</span> <span class="nx">socket</span><span class="p">.</span><span class="nx">emit</span><span class="p">.</span><span class="nx">apply</span> <span class="nx">socket</span><span class="p">,</span> <span class="nx">arguments</span>
- <span class="nv">broadcast: </span><span class="o">-&gt;</span> <span class="nx">socket</span><span class="p">.</span><span class="nx">broadcast</span><span class="p">.</span><span class="nx">emit</span><span class="p">.</span><span class="nx">apply</span> <span class="nx">socket</span><span class="p">.</span><span class="nx">broadcast</span><span class="p">,</span> <span class="nx">arguments</span></pre></div> </td> </tr> <tr id="section-29"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-29">&#182;</a> </div> <p>TODO: fix pseudo-globals here too.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">locals</span><span class="p">[</span><span class="nx">g</span><span class="p">]</span> <span class="o">=</span> <span class="nb">eval</span><span class="p">(</span><span class="nx">g</span><span class="p">)</span> <span class="k">for</span> <span class="nx">g</span> <span class="k">in</span> <span class="nx">globals_names</span>
+ <span class="nv">broadcast: </span><span class="o">-&gt;</span> <span class="nx">socket</span><span class="p">.</span><span class="nx">broadcast</span><span class="p">.</span><span class="nx">emit</span><span class="p">.</span><span class="nx">apply</span> <span class="nx">socket</span><span class="p">.</span><span class="nx">broadcast</span><span class="p">,</span> <span class="nx">arguments</span></pre></div> </td> </tr> <tr id="section-40"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-40">&#182;</a> </div> <p>TODO: fix pseudo-globals here too.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">locals</span><span class="p">[</span><span class="nx">g</span><span class="p">]</span> <span class="o">=</span> <span class="nb">eval</span><span class="p">(</span><span class="nx">g</span><span class="p">)</span> <span class="k">for</span> <span class="nx">g</span> <span class="k">in</span> <span class="nx">globals_names</span>
<span class="k">for</span> <span class="nx">name</span><span class="p">,</span> <span class="nx">def</span> <span class="k">of</span> <span class="nx">defs</span>
<span class="nx">locals</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span> <span class="o">=</span> <span class="nx">def</span>
@@ -329,7 +332,7 @@
<span class="nv">locals.params = </span><span class="nx">context</span>
<span class="nx">h</span><span class="p">(</span><span class="nx">context</span><span class="p">,</span> <span class="nx">locals</span><span class="p">)</span>
- <span class="p">{</span><span class="nx">app</span><span class="p">,</span> <span class="nx">io</span><span class="p">}</span></pre></div> </td> </tr> <tr id="section-30"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-30">&#182;</a> </div> <p>Takes a function and runs it as a zappa app. Optionally accepts a number for port,
+ <span class="p">{</span><span class="nx">id</span><span class="p">,</span> <span class="nx">app</span><span class="p">,</span> <span class="nx">io</span><span class="p">}</span></pre></div> </td> </tr> <tr id="section-41"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-41">&#182;</a> </div> <p>Takes a function and runs it as a zappa app. Optionally accepts a number for port,
and/or a string for hostname (any order).
Returns an object where <code>app</code> is the express server and <code>io</code> is the socket.io handle.
Ex.:
@@ -359,7 +362,7 @@
<span class="nx">log</span> <span class="s2">&quot;Zappa #{zappa.version} orchestrating the show&quot;</span>
- <span class="nx">zapp</span></pre></div> </td> </tr> <tr id="section-31"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-31">&#182;</a> </div> <p>Creates a zappa view adapter for templating engine <code>engine</code>. This adapter
+ <span class="nx">zapp</span></pre></div> </td> </tr> <tr id="section-42"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-42">&#182;</a> </div> <p>Creates a zappa view adapter for templating engine <code>engine</code>. This adapter
can be used with <code>app.register</code> and creates params "shortcuts".</p>
<p>Zappa, by default, automatically sends all request params to templates,
View
3 package.json
@@ -8,7 +8,8 @@
"express": "2.4.3",
"socket.io": "0.7.7",
"jsdom": "0.2.1",
- "coffeekup": "0.3.0beta"
+ "coffeekup": "0.3.0beta",
+ "node-uuid": "1.2.0"
},
"keywords": ["framework", "websockets", "coffeescript", "express", "socket.io", "sammy", "sinatra"],
"main": "./lib/zappa",
View
70 src/zappa.coffee
@@ -7,6 +7,7 @@ zappa = version: '0.2.0beta2'
log = console.log
fs = require 'fs'
path = require 'path'
+uuid = require 'node-uuid'
express = require 'express'
socketio = require 'socket.io'
jsdom = require 'jsdom'
@@ -47,9 +48,49 @@ rewrite_function = (func, locals_names) ->
# The stringified zappa client.
client = require('./client').build(zappa.version, coffeescript_helpers, rewrite_function)
+# List of all apps created by this running copy of zappa.
+# Keyed by the generated app uuid.
+apps = {}
+
+# Monkeypatch express to support lookup of inline templates. Such is life.
+express.View.prototype.__defineGetter__ 'exists', ->
+ # Zappa's `render` sends the view name as appid|viewname.
+ [appid, view] = @view.split '|'
+
+ # Try to find an inline template.
+ return true if apps[appid]?.views[view]?
+ return true if apps[appid]?.views[path.basename(view)]?
+
+ # Inline template not found, take back the app id.
+ @path = @path.replace appid + '|', ''
+
+ # Normal express behaviour.
+ try
+ fs.statSync(@path)
+ return true
+ catch err
+ return false
+
+express.View.prototype.__defineGetter__ 'contents', ->
+ # Zappa's `render` sends the view name as appid|viewname.
+ [appid, view] = @view.split '|'
+
+ # Try to find an inline template.
+ return apps[appid]?.views[view] if apps[appid]?.views[view]?
+ return apps[appid]?.views[path.basename(view)] if apps[appid]?.views[path.basename(view)]?
+
+ # Inline template not found, take back the app id.
+ @path = @path.replace appid + '|', ''
+
+ # Normal express behaviour.
+ fs.readFileSync @path, 'utf8'
+
# Takes in a function and builds express/socket.io apps based on the rules contained in it.
# The optional data object allows passing variables from the outside to the root scope.
zappa.app = ->
+ id = uuid()
+ apps[id] = {}
+
data = null
root_function = null
@@ -90,25 +131,11 @@ zappa.app = ->
ws_handlers = {}
helpers = {}
defs = {}
- views = {}
+ # Keep inline views at the module level and namespaced by app id
+ # so that the monkeypatched express can look them up.
+ apps[id].views = {}
postrenders = {}
- # Monkeypatch express to support inline templates. Such is life.
- # TODO: this doesn't work for multiple apps.
- express.View.prototype.__defineGetter__ 'exists', ->
- return true if views[@view]?
- return true if views[path.basename(@view)]?
- try
- fs.statSync(@path)
- return true
- catch err
- return false
-
- express.View.prototype.__defineGetter__ 'contents', ->
- return views[@view] if views[@view]?
- return views[path.basename(@view)] if views[path.basename(@view)]?
- fs.readFileSync @path, 'utf8'
-
app = express.createServer()
io = socketio.listen(app)
@@ -183,7 +210,7 @@ zappa.app = ->
root_locals.view = (obj) ->
for k, v of obj
- views[k] = v
+ apps[id].views[k] = v
root_locals.set = (obj) ->
for k, v of obj
@@ -341,6 +368,11 @@ zappa.app = ->
locals.redirect = -> res.redirect.apply res, arguments
locals.render = (args...) ->
+ # Adds the app id to the view name so that the monkeypatched
+ # express.View.exists and express.View.contents can lookup
+ # this app's inline templates.
+ args[0] = id + '|' + args[0]
+
# Make sure the second arg is an object.
args[1] ?= {}
args.splice 1, 0, {} if typeof args[1] is 'function'
@@ -407,7 +439,7 @@ zappa.app = ->
locals.params = context
h(context, locals)
- {app, io}
+ {id, app, io}
# Takes a function and runs it as a zappa app. Optionally accepts a number for port,
# and/or a string for hostname (any order).

0 comments on commit d1ed1f6

Please sign in to comment.
Something went wrong with that request. Please try again.