Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
690 lines (620 sloc) 33.3 KB
<a href="http://github.com/weepy/mmmodel"><img alt="Fork me on GitHub" id="ribbon" src="http://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png"></a><html>
<head>
<title>mmmodel</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<style>body {
margin: 0;
padding: 0;
font: 14px/1.5 'Palatino Linotype', 'Book Antiqua', Palatino, FreeSerif, serif;
color: #252519;
}
a {
color: #252519;
}
a:hover {
text-decoration: underline;
color: #19469D;
}
p {
margin: 12px 0;
}
h1, h2, h3 {
margin: 0;
padding: 0;
}
table#source {
width: 100%;
border-collapse: collapse;
}
table#source td:first-child {
padding: 30px 40px 30px 40px;
vertical-align: top;
}
table#source td:first-child,
table#source td:first-child pre {
width: 450px;
}
table#source td:last-child {
padding: 30px 0 30px 40px;
border-left: 1px solid #E5E5EE;
background: #F5F5FF;
}
table#source tr {
border-bottom: 1px solid #E5E5EE;
}
table#source tr.filename {
padding-top: 40px;
border-top: 1px solid #E5E5EE;
}
table#source tr.filename td:first-child {
text-transform: capitalize;
}
table#source tr.filename td:last-child {
font-size: 12px;
}
table#source tr.filename h2 {
margin: 0;
padding: 0;
cursor: pointer;
}
table#source tr.code h1,
table#source tr.code h2,
table#source tr.code h3 {
margin-top: 30px;
font-family: "Lucida Grande", "Helvetica Nueue", Arial, sans-serif;
font-size: 18px;
}
table#source tr.code h2 {
font-size: 16px;
}
table#source tr.code h3 {
font-size: 14px;
}
table#source tr.code ul {
margin: 15px 0 15px 35px;
padding: 0;
}
table#source tr.code ul li {
margin: 0;
padding: 1px 0;
}
table#source tr.code ul li p {
margin: 0;
padding: 0;
}
table#source tr.code td:first-child pre {
padding: 20px;
}
#ribbon {
position: fixed;
top: 0;
right: 0;
}
code .string { color: #219161; }
code .regexp { color: #219161; }
code .keyword { color: #954121; }
code .number { color: #19469D; }
code .comment { color: #bbb; }
code .this { color: #19469D; }</style>
<script>
$(function(){
$('tr.code').hide();
$('tr.filename').toggle(function(){
$(this).nextUntil('.filename').fadeIn();
}, function(){
$(this).nextUntil('.filename').fadeOut();
});
});
</script>
</head>
<body>
<table id="source"><tbody><tr><td><h1>mmmodel</h1><p>Homer Simpson's favoriate Javascript ORM</p></td><td></td></tr><tr class="filename"><td><h2 id="lib/core.js"><a href="#">core</a></h2></td><td>lib/core.js</td></tr><tr class="code">
<td class="docs">
<h2>NB private functions are indicated by an underscore prefix.</h2>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>String</em> name of the class</p></li><li><p><strong>param</strong>: <em>Properties</em> list of property definitions</p></li><li><p><strong>param</strong>: <em>String</em> name of store, e.g. "redis"</p></li></ul>
</td>
<td class="code">
<pre><code><span class="variable">module</span>.<span class="variable">exports</span> = <span class="keyword">function</span>(<span class="variable">type</span>, <span class="variable">properties</span>, <span class="variable">store</span>) {</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Constructor for model. Normally is unsynchronized (i.e. unsaved)</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>Object</em> hash of properties</p></li><li><p><strong>param</strong>: <em>boolean</em> [private] True if the new object is synchonized (i.e. appear to be saved)</p><p></p></li></ul>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="class">Model</span> = <span class="keyword">function</span>(<span class="variable">props</span>, <span class="variable">_sync</span>) {
<span class="variable">props</span> = <span class="variable">props</span> || {}
<span class="this">this</span>.<span class="variable">_.properties</span> = {} <span class="comment">// this._.properties contains the synchronized version of the properties in the Store</span>
<span class="keyword">for</span>(<span class="keyword">var</span> <span class="variable">k</span> <span class="keyword">in</span> <span class="variable">properties</span>) {
<span class="keyword">var</span> <span class="variable">p</span> = <span class="variable">properties</span>[<span class="variable">k</span>]
<span class="keyword">if</span>(<span class="variable">k</span> <span class="keyword">in</span> <span class="variable">props</span>) <span class="this">this</span>[<span class="variable">k</span>] = <span class="variable">props</span>[<span class="variable">k</span>]
<span class="keyword">else</span> <span class="keyword">if</span>(<span class="string">'default'</span> <span class="keyword">in</span> <span class="variable">p</span>) <span class="this">this</span>[<span class="variable">k</span>] = <span class="variable">p</span>[<span class="string">'default'</span>]
}
<span class="this">this</span>.<span class="variable">_.callbacks</span> = {} <span class="comment">// this._.callbacks contains event bindings relevant to this instance</span>
<span class="keyword">if</span>(<span class="variable">props</span>.<span class="variable">errors</span>) <span class="this">this</span>.<span class="variable">errors</span> = <span class="variable">props</span>.<span class="variable">errors</span>
<span class="this">this</span>.<span class="variable">trigger</span>(&<span class="variable">quot</span>;<span class="variable">initialize</span>&<span class="variable">quot</span>;)
<span class="keyword">if</span>(<span class="variable">_sync</span>) <span class="this">this</span>.<span class="variable">_synchronize</span>()
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p> Create a sychronized object (typically it would be one that been retrieved from a database)
</p>
</td>
<td class="code">
<pre><code><span class="class">Model</span>.<span class="variable">load</span> = <span class="keyword">function</span>(<span class="variable">props</span>) {
<span class="keyword">return</span> <span class="keyword">new</span> <span class="class">Model</span>(<span class="variable">props</span>, <span class="variable">true</span>)
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Load a list of models expressed as JSON
</p>
</td>
<td class="code">
<pre><code><span class="class">Model</span>.<span class="variable">load_multi</span> = <span class="keyword">function</span>(<span class="variable">a</span>) {
<span class="keyword">var</span> <span class="variable">ret</span> = []
<span class="keyword">for</span>(<span class="keyword">var</span> <span class="variable">i</span>=<span class="number integer">0</span>;<span class="variable">i</span>&<span class="variable">lt</span>;<span class="variable">a</span>.<span class="variable">length</span>;<span class="variable">i</span>++) <span class="variable">ret</span>[<span class="variable">i</span>] = <span class="class">Model</span>.<span class="variable">load</span>(<span class="variable">a</span>[<span class="variable">i</span>])
<span class="keyword">return</span> <span class="variable">ret</span>
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p> Create a new object that's then saved to the database </p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>Object</em> </p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Model</span>.<span class="variable">create</span> = <span class="keyword">function</span>(<span class="variable">props</span>, <span class="variable">cb</span>) {
<span class="keyword">var</span> <span class="variable">m</span> = <span class="keyword">new</span> <span class="class">Model</span>(<span class="variable">props</span>)
<span class="variable">m</span>.<span class="variable">save</span>(<span class="variable">cb</span> || <span class="variable">noop</span>)
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Find an object or create it if it doesn't exist
</p>
</td>
<td class="code">
<pre><code><span class="class">Model</span>.<span class="variable">find_or_create</span> = <span class="keyword">function</span>(<span class="variable">props</span>, <span class="variable">cb</span>) {
<span class="class">Model</span>.<span class="variable">find</span>(<span class="variable">props</span>.<span class="variable">id</span>, <span class="keyword">function</span>(<span class="variable">m</span>) {
<span class="variable">m</span> ? <span class="variable">cb</span>(<span class="variable">m</span>) : <span class="class">Model</span>.<span class="variable">create</span>(<span class="variable">props</span>, <span class="variable">cb</span> || <span class="variable">noop</span>)
})
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Has this instance been modified (i.e not in sync wit the Store). Returns null if there's no difference</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>string</em> <em>optional</em> if null, we'll check the whole function, else it will check only this property</p><p></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Model</span>.<span class="variable">prototype</span>.<span class="variable">modified</span> = <span class="keyword">function</span>(<span class="variable">prop</span>) {
<span class="keyword">var</span> <span class="variable">o</span> = {}, <span class="variable">modified</span>
<span class="keyword">if</span>(<span class="variable">prop</span>) {
<span class="variable">modified</span> = <span class="this">this</span>.<span class="variable">_.properties</span>[<span class="variable">prop</span>] != <span class="this">this</span>[<span class="variable">prop</span>]
<span class="keyword">return</span> <span class="variable">modified</span> ? [<span class="this">this</span>.<span class="variable">_.properties</span>[<span class="variable">prop</span>], <span class="this">this</span>[<span class="variable">prop</span>]] : <span class="variable">false</span>
}
<span class="keyword">for</span>(<span class="keyword">var</span> <span class="variable">i</span> <span class="keyword">in</span> <span class="variable">properties</span>) {
<span class="keyword">if</span>(<span class="this">this</span>.<span class="variable">_.properties</span>[<span class="variable">i</span>] !== <span class="this">this</span>[<span class="variable">i</span>]) {
<span class="variable">modified</span> = <span class="variable">true</span>
<span class="variable">o</span>[<span class="variable">i</span>] = [<span class="this">this</span>.<span class="variable">_.properties</span>[<span class="variable">i</span>], <span class="this">this</span>[<span class="variable">i</span>]]
}
}
<span class="keyword">return</span> <span class="variable">modified</span> ? <span class="variable">o</span> : <span class="variable">false</span>
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Has this model been saved? I.e. is it <em>not</em> modified
</p>
</td>
<td class="code">
<pre><code><span class="class">Model</span>.<span class="variable">prototype</span>.<span class="variable">saved</span> = <span class="keyword">function</span>() {
<span class="keyword">return</span> !<span class="this">this</span>.<span class="variable">modified</span>()
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Update the model with the params and save it
</p>
</td>
<td class="code">
<pre><code><span class="class">Model</span>.<span class="variable">prototype</span>.<span class="variable">update</span> = <span class="keyword">function</span>(<span class="variable">params</span>, <span class="variable">cb</span>) {
<span class="variable">params</span> || (<span class="variable">params</span> == {})
<span class="variable">cb</span> || (<span class="variable">cb</span> == <span class="variable">noop</span>)
<span class="class">Model</span>.<span class="variable">find</span>(<span class="variable">params</span>.<span class="variable">id</span>, <span class="keyword">function</span>(<span class="variable">m</span>) {
<span class="keyword">if</span>(!<span class="variable">m</span>) {
<span class="variable">cb</span>()
} <span class="keyword">else</span> {
<span class="variable">m</span>.<span class="variable">merge</span>(<span class="variable">params</span>)
<span class="variable">m</span>.<span class="variable">save</span>(<span class="keyword">function</span>(<span class="variable">ok</span>) {
<span class="variable">cb</span>.<span class="variable">call</span>(<span class="variable">m</span>, <span class="variable">ok</span>)
})
}
})
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Save this instance to the store</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>Function</em> optional callback. Returns the saved object</p><p></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Model</span>.<span class="variable">prototype</span>.<span class="variable">save</span> = <span class="keyword">function</span>(<span class="variable">cb</span>) {
<span class="keyword">var</span> <span class="variable">self</span> = <span class="this">this</span>
<span class="variable">cb</span> || (<span class="variable">cb</span> = <span class="variable">noop</span>)
<span class="variable">utils</span>.<span class="variable">achain</span>.<span class="variable">call</span>(<span class="variable">self</span>, <span class="variable">self</span>.<span class="variable">_saveStack</span>, [], <span class="keyword">function</span>(<span class="variable">err</span>, <span class="variable">results</span>) {
<span class="keyword">if</span>(!<span class="variable">err</span>) <span class="variable">self</span>.<span class="variable">_synchronize</span>()
<span class="variable">cb</span>.<span class="variable">call</span>(<span class="variable">self</span>, <span class="variable">self</span>)
})
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Add an error<br></br> </p>
</td>
<td class="code">
<pre><code><span class="class">Model</span>.<span class="variable">prototype</span>.<span class="variable">error</span> = <span class="keyword">function</span>(<span class="variable">error</span>) {
<span class="this">this</span>.<span class="variable">errors</span> || (<span class="this">this</span>.<span class="variable">errors</span> = [])
<span class="this">this</span>.<span class="variable">errors</span>.<span class="variable">push</span>(<span class="variable">error</span>)
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Destroy the object in the store</p>
<h2></h2>
<ul><li><strong>param</strong>: <em>Function</em> optional callback - returns if it was successful or not</li></ul>
<p>if successful triggers "destroyed"
</p>
</td>
<td class="code">
<pre><code><span class="class">Model</span>.<span class="variable">prototype</span>.<span class="variable">destroy</span> = <span class="keyword">function</span>(<span class="variable">cb</span>) {
<span class="keyword">var</span> <span class="variable">self</span> = <span class="this">this</span>
<span class="variable">cb</span> || (<span class="variable">cb</span> = <span class="variable">noop</span>)
<span class="class">Model</span>.<span class="variable">destroy</span>(<span class="this">this</span>.<span class="variable">id</span>, <span class="keyword">function</span>(<span class="variable">ok</span>) {
<span class="keyword">if</span>(<span class="variable">ok</span>) <span class="variable">self</span>.<span class="variable">trigger</span>(&<span class="variable">quot</span>;<span class="variable">destroyed</span>&<span class="variable">quot</span>;)
<span class="variable">cb</span>(<span class="variable">ok</span>)
})
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Returns a object ready for JSON.stringify
NB Does not return a string
NB Date types are converted to numbers
</p>
</td>
<td class="code">
<pre><code><span class="class">Model</span>.<span class="variable">prototype</span>.<span class="variable">toJSON</span> = <span class="keyword">function</span>() {
<span class="keyword">var</span> <span class="variable">o</span> = {}
<span class="keyword">for</span>(<span class="keyword">var</span> <span class="variable">name</span> <span class="keyword">in</span> <span class="variable">properties</span>) {
<span class="keyword">if</span>(<span class="variable">name</span> <span class="keyword">in</span> <span class="this">this</span>) {
<span class="variable">o</span>[<span class="variable">name</span>] = <span class="this">this</span>[<span class="variable">name</span>]
<span class="keyword">if</span>(<span class="variable">properties</span>[<span class="variable">name</span>].<span class="variable">type</span> == &<span class="variable">quot</span>;<span class="variable">date</span>&<span class="variable">quot</span>;) <span class="variable">o</span>[<span class="variable">name</span>] = <span class="variable">o</span>[<span class="variable">name</span>]/<span class="number integer">1</span>
}
}
<span class="variable">o</span>.<span class="variable">type</span> = <span class="variable">type</span>
<span class="keyword">if</span>(<span class="this">this</span>.<span class="variable">in_error</span>()) <span class="variable">o</span>.<span class="variable">errors</span> = <span class="this">this</span>.<span class="variable">errors</span>
<span class="keyword">return</span> <span class="variable">o</span>
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Does this model have errors ?
</p>
</td>
<td class="code">
<pre><code><span class="class">Model</span>.<span class="variable">prototype</span>.<span class="variable">in_error</span> = <span class="keyword">function</span>() {
<span class="keyword">return</span> <span class="this">this</span>.<span class="variable">errors</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="this">this</span>.<span class="variable">errors</span>.<span class="variable">length</span> != <span class="number integer">0</span>
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Merge a list of properties on this the model</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>Object</em> properties</p></li><li><p><strong>param</strong>: <em>Object</em> optional alternative for "this"</p><p></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Model</span>.<span class="variable">prototype</span>.<span class="variable">merge</span> = <span class="keyword">function</span>(<span class="variable">props</span>, <span class="variable">x</span>) {
<span class="keyword">for</span>(<span class="keyword">var</span> <span class="variable">name</span> <span class="keyword">in</span> <span class="variable">properties</span>)
<span class="keyword">if</span>(<span class="variable">name</span> <span class="keyword">in</span> <span class="variable">props</span>) (<span class="variable">x</span> || <span class="this">this</span>)[<span class="variable">name</span>] = <span class="variable">props</span>[<span class="variable">name</span>]
<span class="keyword">return</span> <span class="this">this</span>
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Validate the model for errors
performs some simple checks for 'required' and 'format' properties
calls a "saving" callback, which on completion checks that the number of errors is zero and runs the callback
</p>
</td>
<td class="code">
<pre><code><span class="class">Model</span>.<span class="variable">prototype</span>.<span class="variable">validate</span> = <span class="keyword">function</span>(<span class="variable">cb</span>) {
<span class="this">this</span>.<span class="variable">errors</span> = []
<span class="this">this</span>.<span class="variable">_run_default_validations</span>()
<span class="this">this</span>.<span class="variable">trigger</span>(&<span class="variable">quot</span>;<span class="variable">saving</span>&<span class="variable">quot</span>;).<span class="variable">complete</span>(<span class="keyword">function</span>() {
<span class="variable">cb</span>.<span class="variable">call</span>(<span class="this">this</span>, !<span class="this">this</span>.<span class="variable">in_error</span>())
})
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Simple Validation for 'required' and 'format' properties
</p>
</td>
<td class="code">
<pre><code><span class="class">Model</span>.<span class="variable">prototype</span>.<span class="variable">_run_default_validations</span> = <span class="keyword">function</span>() {
<span class="keyword">for</span>(<span class="keyword">var</span> <span class="variable">i</span> <span class="keyword">in</span> <span class="variable">properties</span>) {
<span class="keyword">var</span> <span class="variable">p</span> = <span class="variable">properties</span>[<span class="variable">i</span>]
<span class="keyword">if</span>(<span class="variable">p</span>.<span class="variable">required</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="this">this</span>[<span class="variable">i</span>] == <span class="keyword">null</span>)
<span class="this">this</span>.<span class="variable">error</span>(<span class="variable">i</span> + &<span class="variable">quot</span>; <span class="variable">is</span> <span class="variable">required</span>&<span class="variable">quot</span>;)
<span class="keyword">if</span>(<span class="variable">p</span>.<span class="variable">format</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="this">this</span>[<span class="variable">i</span>] &<span class="variable">amp</span>;&<span class="variable">amp</span>; !<span class="this">this</span>[<span class="variable">i</span>].<span class="variable">match</span>(<span class="variable">p</span>.<span class="variable">format</span>))
<span class="this">this</span>.<span class="variable">error</span>(<span class="variable">i</span> + &<span class="variable">quot</span>; <span class="variable">is</span> <span class="variable">bad</span> <span class="variable">format</span>&<span class="variable">quot</span>;)
}
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p> Bind to all instances of Model or just this instance
## </p>
<ul><li><p><strong>param</strong>: <em>string</em> name of event, e.g. "saving"</p></li><li><p><strong>param</strong>: <em>Function</em> callback to run</p></li><li><p><strong>param</strong>: <em>boolean</em> is the callback async? </p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Model</span>.<span class="variable">bind</span> = <span class="class">Model</span>.<span class="variable">prototype</span>.<span class="variable">bind</span> = <span class="keyword">function</span>(<span class="variable">ev</span>, <span class="variable">callback</span>, <span class="variable">async</span>) {
<span class="keyword">if</span>(<span class="variable">async</span>) <span class="variable">callback</span>.<span class="variable">async</span> = <span class="variable">true</span>
<span class="keyword">var</span> <span class="variable">list</span> = <span class="this">this</span>.<span class="variable">_.callbacks</span>[<span class="variable">ev</span>] || (<span class="this">this</span>.<span class="variable">_.callbacks</span>[<span class="variable">ev</span>] = [])
<span class="variable">list</span>.<span class="variable">push</span>(<span class="variable">callback</span>)
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p> Unbind to all instances of Model or just this instance </p>
</td>
<td class="code">
<pre><code><span class="class">Model</span>.<span class="variable">unbind</span> = <span class="class">Model</span>.<span class="variable">prototype</span>.<span class="variable">unbind</span> = <span class="keyword">function</span>(<span class="variable">ev</span>, <span class="variable">callback</span>) {
<span class="keyword">if</span> (!<span class="variable">ev</span>)
<span class="this">this</span>.<span class="variable">_.callbacks</span> = {}
<span class="keyword">else</span> <span class="keyword">if</span> (!<span class="variable">callback</span>)
<span class="this">this</span>.<span class="variable">_.callbacks</span>[<span class="variable">ev</span>] = []
<span class="keyword">else</span> {
<span class="keyword">var</span> <span class="variable">list</span> = <span class="this">this</span>.<span class="variable">_.callbacks</span>[<span class="variable">ev</span>] || []
<span class="keyword">for</span> (<span class="keyword">var</span> <span class="variable">i</span> = <span class="number integer">0</span>; <span class="variable">i</span> &<span class="variable">lt</span>; <span class="variable">list</span>.<span class="variable">length</span>; <span class="variable">i</span>++) {
<span class="keyword">if</span> (<span class="variable">callback</span> === <span class="variable">list</span>[<span class="variable">i</span>]) {
<span class="variable">list</span>.<span class="variable">splice</span>(<span class="variable">i</span>, <span class="number integer">1</span>)
<span class="keyword">break</span>
}
}
}
<span class="keyword">return</span> <span class="this">this</span>
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p> Trigger an event. This will trigger all handers with the particular event
It will also return a promise that allows a complete callback to be set.
## </p>
<ul><li><p><strong>param</strong>: <em>string</em> name of event</p></li><li><p><strong>para</strong>: <em>m</em>
[further arugments]</p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Model</span>.<span class="variable">prototype</span>.<span class="variable">trigger</span> = <span class="keyword">function</span>(<span class="variable">ev</span>) <span class="comment">// further args </span>
{
<span class="keyword">var</span> <span class="variable">global</span> = <span class="class">Model</span>.<span class="variable">_.callbacks</span>[<span class="variable">ev</span>] || []
<span class="variable">local</span> = <span class="this">this</span>.<span class="variable">_.callbacks</span>[<span class="variable">ev</span>] || [],
<span class="variable">list</span> = <span class="variable">global</span>.<span class="variable">concat</span>(<span class="variable">local</span>)
<span class="variable">args</span> = <span class="class">Array</span>.<span class="variable">prototype</span>.<span class="variable">slice</span>.<span class="variable">call</span>(<span class="variable">arguments</span>, <span class="number integer">1</span>),
<span class="variable">self</span> = <span class="this">this</span>
<span class="keyword">var</span> <span class="variable">pr</span> = <span class="keyword">new</span> (<span class="variable">utils</span>.<span class="variable">promise</span>)(<span class="variable">self</span>)
<span class="variable">utils</span>.<span class="variable">chain</span>.<span class="variable">call</span>(<span class="variable">self</span>, <span class="variable">list</span>, <span class="variable">args</span>, <span class="keyword">function</span>(<span class="variable">err</span>, <span class="variable">results</span>) {
<span class="variable">nextTick</span>(<span class="keyword">function</span>() {
<span class="variable">pr</span>.<span class="variable">invoke</span>(<span class="variable">err</span>, <span class="variable">results</span>)
}, <span class="number integer">0</span>)
})
<span class="keyword">return</span> <span class="variable">pr</span>
}
<span class="keyword">return</span> <span class="class">Model</span>
}
</code></pre>
</td>
</tr><tr class="filename"><td><h2 id="lib/edge_query.js"><a href="#">edge_query</a></h2></td><td>lib/edge_query.js</td></tr><tr class="code">
<td class="docs">
<p>function EdgeQuery(x, self) {
this.key = x
this.self = self
}</p>
<p>EdgeQuery.prototype.range = function(low, high) {
high != null || (high = low)
this._range = [low, high]
return this
}</p>
<p>EdgeQuery.prototype.count = function(cb) {
this._count = true
this.all(cb)
}</p>
<p>EdgeQuery.prototype.all = function(cb) {<br></br> var args = [this.key]
var method
if(this.<em>range) {
method = this.</em>count ? "ZCOUNT" : "ZRANGEBYSCORE"
} else {
if(this._count)
method = "ZCARD"
else {
method = "ZRANGE"
args.push("0")
args.push("-1")
}
}</p>
<p> if(this.<em>range) {
var L = this.</em>range[0].toString().replace(/()/g, "").replace("inf", "Infinity")
var R = this._range[1].toString().replace(/()/g, "").replace("inf", "Infinity")</p>
<pre><code>L = parseFloat(L)
R = parseFloat(R)
if(R &lt; L) { // swap !
method = method.replace("ZRANGE","ZREVRANGE")
// this._range = [this._range[1], this._range[0]]
}
args = args.concat(this._range)</code></pre>
<p> }</p>
<p> var context = this.self</p>
<p> var klass = this.<em>load</em>as</p>
<p> args.push(function(err, data) {
if(err) return cb.call(context, null)
if(klass) klass.load_ids(data, cb)
else cb.call(context, data)
})</p>
<p> var client = self.constructor.client</p>
<p> client[method].apply(client, args)
}</p>
<p>EdgeQuery.prototype.load<em>as = function(type) {
this.</em>load_as = type
return this
}</p>
<p>// EdgeQuery.prototype.desc = function() {
// this._desc = true
// return this
// }</p>
<p>module.exports = EdgeQuery</p>
</td>
<td class="code">
</td>
</tr><tr class="filename"><td><h2 id="lib/mmmodel.js"><a href="#">mmmodel</a></h2></td><td>lib/mmmodel.js</td></tr><tr class="code">
<td class="docs">
<p>exports.core = require("./core")</p>
<p>exports.redis = function(type, properties) {
return exports.core(type, properties, "redis")
}</p>
<p>exports.rest = function(type, properties) {
return exports.core(type, properties, "rest")
}</p>
<p>exports.memory = function(type, properties) {
return exports.core(type, properties, "memory")
}</p>
</td>
<td class="code">
</td>
</tr><tr class="filename"><td><h2 id="lib/queue.js"><a href="#">queue</a></h2></td><td>lib/queue.js</td></tr><tr class="code">
<td class="docs">
<p>function Q() {
this.jobs = []
return
}</p>
<p>Q.prototype._run = function() {
if(this.current) return
this.current = this.jobs.shift()<br></br> if(!this.current) return
this.current.job.call(this) //() //.call this.current.context
}</p>
<p>Q.prototype.complete = function(result) {<br></br> console.log("completeing queue")
if(!this.current) {
console.log("error result - when not running?!")
return
}
if(this.current.callback) this.current.callback(result) //.call(this.current.context, result)<br></br> this.current = null
this._run()
}</p>
<p>Q.prototype.add = function(job, callback) {
console.log("adding to queue")
this.jobs.push({job: job, callback: callback})
this._run()
}</p>
<p>module.exports = Q</p>
</td>
<td class="code">
</td>
</tr><tr class="filename"><td><h2 id="lib/utils.js"><a href="#">utils</a></h2></td><td>lib/utils.js</td></tr><tr class="code">
<td class="docs">
<p>var nextTick = (typeof process != "undefined") ? process.nextTick : setTimeout</p>
<p>exports.merge = function(self, o) {
for(var i in o) self[i] = o[i]
return self
}</p>
<p>function Promise(context) {
this.context = context
}
Promise.prototype.invoke = function() {
if(!this.<em>complete) return
this.</em>complete.apply(this.context, arguments)
}
Promise.prototype.complete = function(cb) {
this._complete = cb
}</p>
<p>exports.promise = Promise</p>
<p>exports.achain = function achain(fns, args, done) {
for(var i in fns) fns[i].async = true
exports.chain.call(this, fns, args, done)
}</p>
<p>exports.chain = function chain(fns, args, done) {
var self = this,
results = [],
i = 0</p>
<p> args || (args = [])
function complete(err, results) {
done.call(self, err, results )
}</p>
<p> function result(answer){
if(answer === false) return complete(true, results)
results[i] = answer</p>
<pre><code>i++
if(i == fns.length) return complete(null, results)
run(fns[i])</code></pre>
<p> }</p>
<p> var async_args = args.concat([result])</p>
<p> function run(fn) {
if(fn.async)
fn.apply(self, async_args)
else {
var n = fn.apply(self, args)
result(n)
}
}</p>
<p> if(fns.length == 0) return complete(null, results)
run(fns[0])
}</p>
<p>function Q() {
this.jobs = []
return
}</p>
<p>Q.prototype._run = function() {
if(this.current) return
this.current = this.jobs.shift()<br></br> if(!this.current) return
this.current.job.call(this) //() //.call this.current.context
}</p>
<p>Q.prototype.complete = function(result) {<br></br> if(!this.current) {
console.log("error result - when not running?!")
return
}
if(this.current.callback) this.current.callback(result) //.call(this.current.context, result)<br></br> this.current = null
this._run()
}</p>
<p>Q.prototype.add = function(job, callback) {
this.jobs.push({job: job, callback: callback})
this._run()
}</p>
<p>exports.queue = Q</p>
</td>
<td class="code">
</td>
</tr> </body>
</html></tbody></table>