-
Notifications
You must be signed in to change notification settings - Fork 70
/
index.html
427 lines (362 loc) · 33.6 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
<!DOCTYPE html>
<!--
**********************************************************************
*
* DO NOT MODIFY THIS FILE IF ITS NAME DOES NOT START WITH '_' !
*
* Instead change the corresponding _file.md or _file.html in docs/
* and follow the build instructions in docs/README.md
*
**********************************************************************
-->
<html>
<head>
<title>
Agility.js Javascript MVC library
</title>
<base href="docs/" target="_self" />
<meta charset="utf-8" />
<meta name="description" content="Agility.js is an MVC library for Javascript that lets you write maintainable and reusable browser code without the verbose or infrastructural overhead found in other MVC libraries."/>
<meta name="author" content="Artur Adib"/>
<link rel="icon" type="image/png" href="favicon.png">
<link type="text/css" href="highlight.css" rel="stylesheet" />
<style type="text/css">
html,body { background:#ebebeb; margin:0; padding:0; padding-bottom:15px; font-family:'Palatino Linotype', 'Book Antiqua', Palatino, FreeSerif, serif; font-size:16px; line-height:150%; }
pre { line-height:125%; }
div.center { width:850px; margin-left:auto; margin-right:auto; margin-top:30px; }
a { color:#0000A0; text-decoration:none; }
a:hover { text-decoration:underline; }
div#decor { height:8px; background:#0000A0; }
div#top { font-size:19px; color:#333; }
div#menu { text-align:center; }
div#menu li { display:inline; margin-right:40px; }
div#box {
margin-top:0;
background:white;
-moz-border-radius: 3px;
-webkit-border-radius: 3px;
border-radius: 3px;
-moz-box-shadow: 0px 0px 8px #999;
-webkit-box-shadow: 0px 0px 8px #999;
box-shadow: 0px 0px 8px #999;
}
div#box-wrapper { padding:10px 20px; background:white; }
div#box-wrapper .download { background:#FFFFCC; padding:5px 10px; text-align:center;}
.demo { margin-top:-10px; margin-left:10px; font-size:80%; }
div#demo-wrapper { font-size:90%; font-family:Arial, sans; }
div#demo-wrapper #msg { margin-bottom:10px; }
div#demo-wrapper iframe { width:600px; height:400px; border:1px solid #333; }
#simplemodal-overlay { background-color:#000; }
#simplemodal-container { background-color:white; border:8px solid #555; padding:12px; }
</style>
<script type="text/javascript">
if (window.location.href.search(/\?x/) < 0) {
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-924459-9']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
}
</script>
<!-- Google+ -->
<link href="https://plus.google.com/116251025970928820842" rel="publisher" />
</head>
<body>
<div id="decor"></div>
<a href="http://github.com/arturadib/agility"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_orange_ff7600.png" alt="Fork me on GitHub"></a>
<div id="top" class='center'>
<div style="float:left;">
<div><img src="logo.png"/></div>
<div>Ship more. Code and maintain less.</div>
</div>
<div style="float:right;">
<div>
<a href="https://twitter.com/share" class="twitter-share-button" data-url="http://agilityjs.com" data-text="Agility.js: JavaScript MVC for the "write less, do more" programmer. Ship more. Code and maintain less" data-via="agilityjs" data-lang="en" data-size="large" style="border:none;">Tweet</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");
</script>
<div class="g-plusone" data-href="http://agilityjs.com"></div>
<script type="text/javascript">
(function() {
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
po.src = 'https://apis.google.com/js/plusone.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();
</script>
</div>
<div style="font-size:14px; line-height:130%; margin-top:10px;">
Twitter: <a href="http://twitter.com/r2r">@r2r</a> <br/>
Mailing list: <a href="http://groups.google.com/group/agilityjs">Google Groups</a> <br/>
</div>
</div>
<div style="clear:both;"></div>
</div>
<div id="menu" class='center'>
<ul>
<li><a href="/">Home</a></li>
<li><a href="docs.html">Docs</a></li>
<li><a href="gallery.html">Gallery</a></li>
<li><a href="http://jsbin.com/agility/224/edit#source" target="_blank">Try it online!</a></li>
<li><a href="https://github.com/arturadib/agility" target="_blank"><b>Contribute</b></a></li>
</ul>
</div>
<div id="box" class='center'>
<div id='box-wrapper'>
<!-- __INSERT_POINT__ -->
<p>Agility.js is an <a href="http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller">MVC</a> library for Javascript that lets you write maintainable and reusable browser code without the verbose or infrastructural overhead found in <a href="http://documentcloud.github.com/backbone/">other</a> <a href="http://www.sproutcore.com/">MVC</a> <a href="http://knockoutjs.com/">libraries</a>. The goal is to enable developers to write web apps at least as quickly as with jQuery, while simplifying long-term maintainability through MVC objects.</p>
<div class='download'>
<div><b>Latest version: 0.1.3 (Sep 3, 2012) </b> | <a href="https://raw.github.com/arturadib/agility/master/ChangeLog">ChangeLog</a></div>
<div>Download <a href="agility.min.js">minified js</a> (~4K packed + gzipped)</div>
<div>Download <a href="agility-start.zip">"getting started" package</a> (zip) or <a href="https://github.com/arturadib/agility/tree/master/docs/agility-start">browse its contents</a></div>
</div>
<p>The library itself is a small Javascript file (goal is to remain under 10K), and its only dependency is a recent version of jQuery. The project is licensed under the liberal <a href="https://github.com/arturadib/agility/blob/master/LICENSE">MIT license</a>.</p>
<p>See the documentation for a more complete <a href="docs.html#intro-features">list of features</a>.</p>
<h2>Quick tour</h2>
<p>Agility supports (but does not require) writing your entire code in Javascript, that is, content (HTML), style (CSS), and behavior (JS) can all be contained within Javascript objects. Unless otherwise stated the examples in this tour consist of such code. See <a href="docs.html#getting-started">Getting started</a> in the docs for the HTML template adopted.</p>
<h3>Object initialization</h3>
<p>Agility works with a single object type that contains a full model-view-controller stack. These MVC objects are built by passing initializers to the <a href="docs.html#factory">factory function</a> <code>$$()</code>, which takes care of setting up default bindings, auto-proxying, etc. The example below initializes an MVC object with empty model, a simple view, and default controllers:</p>
<div class="codehilite"><pre><span class="c1">// Hello World</span>
<span class="kd">var</span> <span class="nx">message</span> <span class="o">=</span> <span class="nx">$$</span><span class="p">({</span>
<span class="nx">model</span><span class="o">:</span> <span class="p">{},</span>
<span class="nx">view</span><span class="o">:</span> <span class="p">{</span>
<span class="nx">format</span><span class="o">:</span> <span class="s1">'<div>Hello World</div>'</span>
<span class="p">},</span>
<span class="nx">controller</span><span class="o">:</span> <span class="p">{}</span>
<span class="p">});</span>
<span class="nx">$$</span><span class="p">.</span><span class="nb">document</span><span class="p">.</span><span class="nx">append</span><span class="p">(</span><span class="nx">message</span><span class="p">);</span>
</pre></div>
<p><div class="demo"></div></p>
<p>A more compact syntax is also supported. It's handy when dealing with simple objects:</p>
<div class="codehilite"><pre><span class="c1">// Hello World</span>
<span class="c1">// Compact syntax: initializers are passed in the order M, V, C:</span>
<span class="kd">var</span> <span class="nx">message</span> <span class="o">=</span> <span class="nx">$$</span><span class="p">({},</span> <span class="s1">'<div>Hello World</div>'</span><span class="p">);</span>
<span class="nx">$$</span><span class="p">.</span><span class="nb">document</span><span class="p">.</span><span class="nx">append</span><span class="p">(</span><span class="nx">message</span><span class="p">);</span>
</pre></div>
<p><div class="demo"></div></p>
<h3>External HTML</h3>
<p>Inline HTML is useful for things like quick prototyping, but sometimes it's useful to keep format and behavior separate. In this case you can pull the HTML from a DOM element, like so:</p>
<div class="codehilite"><pre><span class="c1">// Hello World</span>
<span class="kd">var</span> <span class="nx">message</span> <span class="o">=</span> <span class="nx">$$</span><span class="p">({</span>
<span class="nx">model</span><span class="o">:</span> <span class="p">{},</span>
<span class="nx">view</span><span class="o">:</span> <span class="p">{</span>
<span class="nx">format</span><span class="o">:</span> <span class="nx">$</span><span class="p">(</span><span class="s1">'#my-format'</span><span class="p">).</span><span class="nx">html</span><span class="p">()</span>
<span class="p">},</span>
<span class="nx">controller</span><span class="o">:</span> <span class="p">{}</span>
<span class="p">});</span>
<span class="nx">$$</span><span class="p">.</span><span class="nb">document</span><span class="p">.</span><span class="nx">append</span><span class="p">(</span><span class="nx">message</span><span class="p">);</span>
</pre></div>
<p>And then in your HTML file:</p>
<div class="codehilite"><pre><span class="nt"><script</span> <span class="na">id=</span><span class="s">"my-format"</span> <span class="na">type=</span><span class="s">"text/html"</span><span class="nt">></span>
<span class="nt"><div></span>Hello World<span class="nt"></div></span>
<span class="nt"></script></span>
</pre></div>
<h3>Model-view bindings</h3>
<p>Agility comes with a powerful model-view binder, so that views are always in sync with models and vice-versa. Establishing bindings is as simple as introducing a <code>data-bind</code> attribute in the desired DOM element; the factory function takes care of creating the necessary controllers:</p>
<div class="codehilite"><pre><span class="c1">// Bind model to element's HTML content</span>
<span class="kd">var</span> <span class="nx">message</span> <span class="o">=</span> <span class="nx">$$</span><span class="p">({</span><span class="nx">txt</span><span class="o">:</span><span class="s2">"I'm text from a model"</span><span class="p">},</span> <span class="s1">'<div data-bind="txt"/>'</span><span class="p">);</span>
<span class="nx">$$</span><span class="p">.</span><span class="nb">document</span><span class="p">.</span><span class="nx">append</span><span class="p">(</span><span class="nx">message</span><span class="p">);</span>
<span class="c1">// Bind model to element's attribute</span>
<span class="kd">var</span> <span class="nx">url</span> <span class="o">=</span> <span class="s1">'http://google.com/favicon.ico'</span><span class="p">;</span>
<span class="kd">var</span> <span class="nx">icon</span> <span class="o">=</span> <span class="nx">$$</span><span class="p">({</span><span class="nx">path</span><span class="o">:</span><span class="nx">url</span><span class="p">},</span> <span class="s1">'<p>Image src from model: <img data-bind="src=path"/></p>'</span><span class="p">);</span>
<span class="nx">$$</span><span class="p">.</span><span class="nb">document</span><span class="p">.</span><span class="nx">append</span><span class="p">(</span><span class="nx">icon</span><span class="p">);</span>
</pre></div>
<p><div class="demo"></div></p>
<p>Bindings are always two-way (instant updates in both directions), and work with most input types:</p>
<div class="codehilite"><pre><span class="c1">// Two-way binding (checkbox)</span>
<span class="kd">var</span> <span class="nx">check</span> <span class="o">=</span> <span class="nx">$$</span><span class="p">(</span>
<span class="p">{</span><span class="nx">a</span><span class="o">:</span><span class="kc">false</span><span class="p">,</span> <span class="nx">b</span><span class="o">:</span><span class="kc">true</span><span class="p">},</span>
<span class="s2">"<div> \</span>
<span class="s2"> <input type='checkbox' name='test' data-bind='a'/> checked: <span data-bind='a'/><br/> \</span>
<span class="s2"> <input type='checkbox' name='test' data-bind='b'/> checked: <span data-bind='b'/><br/> \</span>
<span class="s2"> </div>"</span>
<span class="p">);</span>
<span class="nx">$$</span><span class="p">.</span><span class="nb">document</span><span class="p">.</span><span class="nx">append</span><span class="p">(</span><span class="nx">check</span><span class="p">);</span>
</pre></div>
<p><div class="demo"></div></p>
<h3>Controller-event bindings</h3>
<p>User-defined controllers are bound to events by automatically matching function and event names. To handle DOM events, start with the event name followed by a DOM selector. Agility events (e.g. object creation, model change, etc) are never followed by a selector:</p>
<div class="codehilite"><pre><span class="kd">var</span> <span class="nx">person</span> <span class="o">=</span> <span class="nx">$$</span><span class="p">({},</span> <span class="s1">'<p><input type="text" data-bind="name"/> <span id="msg"/></p>'</span><span class="p">,</span> <span class="p">{</span>
<span class="s1">'create'</span><span class="o">:</span> <span class="kd">function</span><span class="p">(){</span>
<span class="c1">// Fired upon object creation</span>
<span class="k">this</span><span class="p">.</span><span class="nx">view</span><span class="p">.</span><span class="nx">$</span><span class="p">(</span><span class="s1">'#msg'</span><span class="p">).</span><span class="nx">text</span><span class="p">(</span><span class="s1">'Enter name'</span><span class="p">);</span>
<span class="p">},</span>
<span class="s1">'change'</span><span class="o">:</span> <span class="kd">function</span><span class="p">(){</span>
<span class="c1">// Fired upon model change</span>
<span class="k">this</span><span class="p">.</span><span class="nx">view</span><span class="p">.</span><span class="nx">$</span><span class="p">(</span><span class="s1">'#msg'</span><span class="p">).</span><span class="nx">text</span><span class="p">(</span><span class="s1">'Name changed to: '</span><span class="o">+</span><span class="k">this</span><span class="p">.</span><span class="nx">model</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="s1">'name'</span><span class="p">));</span>
<span class="p">},</span>
<span class="s1">'focus input'</span><span class="o">:</span> <span class="kd">function</span><span class="p">(){</span>
<span class="c1">// Fired upon DOM event 'focus' on element input</span>
<span class="c1">// 'this' is always auto-proxied to the owner MVC object</span>
<span class="k">this</span><span class="p">.</span><span class="nx">view</span><span class="p">.</span><span class="nx">$</span><span class="p">(</span><span class="s1">'#msg'</span><span class="p">).</span><span class="nx">text</span><span class="p">(</span><span class="s1">'Focused on input!'</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">});</span>
<span class="nx">$$</span><span class="p">.</span><span class="nb">document</span><span class="p">.</span><span class="nx">append</span><span class="p">(</span><span class="nx">person</span><span class="p">);</span>
</pre></div>
<p><div class="demo"></div></p>
<h3>Inheritance and containers</h3>
<p>Agility adopts a simple object hierarchy model. Objects can serve as the prototype of other objects (<a href="http://en.wikipedia.org/wiki/Differential_inheritance">differential inheritance</a>):</p>
<div class="codehilite"><pre><span class="c1">// Base object with empty model:</span>
<span class="kd">var</span> <span class="nx">proto</span> <span class="o">=</span> <span class="nx">$$</span><span class="p">({},</span> <span class="s1">'<p data-bind="name" style="color:red"/>'</span><span class="p">);</span>
<span class="c1">// Derived objects with specified models:</span>
<span class="kd">var</span> <span class="nx">obj1</span> <span class="o">=</span> <span class="nx">$$</span><span class="p">(</span><span class="nx">proto</span><span class="p">,</span> <span class="p">{</span><span class="nx">name</span><span class="o">:</span><span class="s1">'Joe Doe'</span><span class="p">});</span>
<span class="kd">var</span> <span class="nx">obj2</span> <span class="o">=</span> <span class="nx">$$</span><span class="p">(</span><span class="nx">proto</span><span class="p">,</span> <span class="p">{</span><span class="nx">name</span><span class="o">:</span><span class="s1">'Foo Bar'</span><span class="p">});</span>
<span class="nx">$$</span><span class="p">.</span><span class="nb">document</span><span class="p">.</span><span class="nx">append</span><span class="p">(</span><span class="nx">obj1</span><span class="p">);</span>
<span class="nx">$$</span><span class="p">.</span><span class="nb">document</span><span class="p">.</span><span class="nx">append</span><span class="p">(</span><span class="nx">obj2</span><span class="p">);</span>
</pre></div>
<p><div class="demo"></div></p>
<p>as well as containers for other Agility objects:</p>
<div class="codehilite"><pre><span class="kd">var</span> <span class="nx">counter</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
<span class="kd">var</span> <span class="nx">item</span> <span class="o">=</span> <span class="nx">$$</span><span class="p">({},</span> <span class="s1">'<li>I\'m item #<span data-bind="id"/></li>'</span><span class="p">);</span>
<span class="kd">var</span> <span class="nx">list</span> <span class="o">=</span> <span class="nx">$$</span><span class="p">({},</span> <span class="s1">'<div> <button>Click me</button> <ul></ul> </div>'</span><span class="p">,</span> <span class="p">{</span>
<span class="s1">'click button'</span><span class="o">:</span> <span class="kd">function</span><span class="p">(){</span>
<span class="kd">var</span> <span class="nx">newItem</span> <span class="o">=</span> <span class="nx">$$</span><span class="p">(</span><span class="nx">item</span><span class="p">,</span> <span class="p">{</span><span class="nx">id</span><span class="o">:</span><span class="nx">counter</span><span class="p">});</span>
<span class="k">this</span><span class="p">.</span><span class="nx">prepend</span><span class="p">(</span><span class="nx">newItem</span><span class="p">,</span> <span class="s1">'ul'</span><span class="p">);</span> <span class="c1">// add object to container, prepend view at <ul></span>
<span class="nx">counter</span><span class="o">++</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">})</span>
<span class="nx">$$</span><span class="p">.</span><span class="nb">document</span><span class="p">.</span><span class="nx">append</span><span class="p">(</span><span class="nx">list</span><span class="p">);</span>
</pre></div>
<p><div class="demo"></div></p>
<h3>In-object content, style, and behavior</h3>
<p>Views don't require styles (CSS) to be declared in-object, but doing so leads to fully reusable objects and eliminates the need to maintain HTML and CSS files. For example, the object <code>clock</code> below can be copied-and-pasted into any Agility project - its content, style, and behavior are preserved without having to fish out HTML and CSS elements/selectors from different files:</p>
<div class="codehilite"><pre><span class="kd">var</span> <span class="nx">clock</span> <span class="o">=</span> <span class="nx">$$</span><span class="p">({</span>
<span class="nx">model</span><span class="o">:</span> <span class="p">{</span>
<span class="nx">time</span><span class="o">:</span> <span class="s1">'12:00:00'</span>
<span class="p">},</span>
<span class="nx">view</span><span class="o">:</span> <span class="p">{</span>
<span class="nx">format</span><span class="o">:</span> <span class="s1">'<span data-bind="time"/>'</span><span class="p">,</span>
<span class="nx">style</span><span class="o">:</span> <span class="s1">'& { color:white; background:#88f; padding:4px 8px; }'</span>
<span class="p">},</span>
<span class="nx">controller</span><span class="o">:</span> <span class="p">{</span>
<span class="s1">'create'</span><span class="o">:</span> <span class="kd">function</span><span class="p">(){</span>
<span class="kd">var</span> <span class="nx">self</span> <span class="o">=</span> <span class="k">this</span><span class="p">;</span>
<span class="nx">setInterval</span><span class="p">(</span><span class="kd">function</span><span class="p">(){</span>
<span class="nx">self</span><span class="p">.</span><span class="nx">model</span><span class="p">.</span><span class="nx">set</span><span class="p">({</span><span class="nx">time</span><span class="o">:</span> <span class="p">(</span><span class="k">new</span> <span class="nb">Date</span><span class="p">()).</span><span class="nx">toLocaleTimeString</span><span class="p">()});</span>
<span class="p">},</span> <span class="mi">1000</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">});</span>
<span class="nx">$$</span><span class="p">.</span><span class="nb">document</span><span class="p">.</span><span class="nx">append</span><span class="p">(</span><span class="nx">clock</span><span class="p">);</span>
</pre></div>
<p><div class="demo"></div></p>
<h3>Persistence</h3>
<p>Server- and client-side persistence can be implemented through the built-in plugin <a href="docs.html#persist">persist</a>. You can setup and <code>load</code> a model from a RESTful service in one line (the view is automatically updated via model-view binding):</p>
<div class="codehilite"><pre><span class="kd">var</span> <span class="nx">person</span> <span class="o">=</span> <span class="nx">$$</span><span class="p">({</span><span class="nx">id</span><span class="o">:</span><span class="mi">123</span><span class="p">},</span> <span class="s1">'<p>Name: <span data-bind="name"/></p>'</span><span class="p">,</span> <span class="s1">'& span {background:blue; color:white; padding:3px 6px;}'</span><span class="p">);</span>
<span class="c1">// Initialize plugin with RESTful adapter, load model with above id:</span>
<span class="nx">person</span><span class="p">.</span><span class="nx">persist</span><span class="p">(</span><span class="nx">$$</span><span class="p">.</span><span class="nx">adapter</span><span class="p">.</span><span class="nx">restful</span><span class="p">,</span> <span class="p">{</span><span class="nx">collection</span><span class="o">:</span><span class="s1">'people'</span><span class="p">}).</span><span class="nx">load</span><span class="p">();</span>
<span class="nx">$$</span><span class="p">.</span><span class="nb">document</span><span class="p">.</span><span class="nx">append</span><span class="p">(</span><span class="nx">person</span><span class="p">);</span>
</pre></div>
<p><div class="demo"></div></p>
<p>(View JSON server response used in the request above: <a href="api/people/123">GET api/people/123</a>).</p>
<p>You can also <code>gather</code> an entire collection of models and insert them as MVC objects (derived from a prototype) into an object's container, as illustrated below. This example also shows how Ajax "loading" icons/messages are implemented through two simple controllers:</p>
<div class="codehilite"><pre><span class="c1">//</span>
<span class="c1">// person: prototype (will be 'gathered' below)</span>
<span class="c1">//</span>
<span class="kd">var</span> <span class="nx">person</span> <span class="o">=</span> <span class="nx">$$</span><span class="p">({},</span> <span class="s1">'<li data-bind="name"/>'</span><span class="p">).</span><span class="nx">persist</span><span class="p">(</span><span class="nx">$$</span><span class="p">.</span><span class="nx">adapter</span><span class="p">.</span><span class="nx">restful</span><span class="p">,</span> <span class="p">{</span><span class="nx">collection</span><span class="o">:</span><span class="s1">'people'</span><span class="p">});</span>
<span class="c1">//</span>
<span class="c1">// people: persons container</span>
<span class="c1">//</span>
<span class="kd">var</span> <span class="nx">people</span> <span class="o">=</span> <span class="nx">$$</span><span class="p">({</span>
<span class="nx">model</span><span class="o">:</span> <span class="p">{},</span>
<span class="nx">view</span><span class="o">:</span> <span class="p">{</span>
<span class="nx">format</span><span class="o">:</span>
<span class="s1">'<div>\</span>
<span class="s1"> <span>Loading ...</span>\</span>
<span class="s1"> <button>Load people</button><br/><br/>\</span>
<span class="s1"> People: <ul/>\</span>
<span class="s1"> </div>'</span><span class="p">,</span>
<span class="nx">style</span><span class="o">:</span>
<span class="s1">'& {position:relative}\</span>
<span class="s1"> & span {position:absolute; top:0; right:0; padding:3px 6px; background:red; color:white; display:none; }'</span>
<span class="p">},</span>
<span class="nx">controller</span><span class="o">:</span> <span class="p">{</span>
<span class="s1">'click button'</span><span class="o">:</span> <span class="kd">function</span><span class="p">(){</span>
<span class="k">this</span><span class="p">.</span><span class="nx">empty</span><span class="p">();</span>
<span class="k">this</span><span class="p">.</span><span class="nx">gather</span><span class="p">(</span><span class="nx">person</span><span class="p">,</span> <span class="s1">'append'</span><span class="p">,</span> <span class="s1">'ul'</span><span class="p">);</span>
<span class="p">},</span>
<span class="c1">// Ajax loading message - start</span>
<span class="s1">'persist:start'</span><span class="o">:</span> <span class="kd">function</span><span class="p">(){</span>
<span class="k">this</span><span class="p">.</span><span class="nx">view</span><span class="p">.</span><span class="nx">$</span><span class="p">(</span><span class="s1">'span'</span><span class="p">).</span><span class="nx">show</span><span class="p">();</span>
<span class="p">},</span>
<span class="c1">// Ajax loading message - stop</span>
<span class="s1">'persist:stop'</span><span class="o">:</span> <span class="kd">function</span><span class="p">(){</span>
<span class="k">this</span><span class="p">.</span><span class="nx">view</span><span class="p">.</span><span class="nx">$</span><span class="p">(</span><span class="s1">'span'</span><span class="p">).</span><span class="nx">hide</span><span class="p">();</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">}).</span><span class="nx">persist</span><span class="p">();</span> <span class="c1">// this makes .gather() available</span>
<span class="nx">$$</span><span class="p">.</span><span class="nb">document</span><span class="p">.</span><span class="nx">append</span><span class="p">(</span><span class="nx">people</span><span class="p">);</span>
</pre></div>
<p><div class="demo"></div></p>
<p>(View JSON server response used in the request above: <a href="api/people/">GET api/people/</a>).</p>
<h3>The infamous To-Do example</h3>
<p>Last but not least, no modern MVC library is complete without a simple To-Do list example. The fully functional example below has 17 lines of code:</p>
<div class="codehilite"><pre><span class="c1">//</span>
<span class="c1">// Item prototype</span>
<span class="c1">//</span>
<span class="kd">var</span> <span class="nx">item</span> <span class="o">=</span> <span class="nx">$$</span><span class="p">({},</span> <span class="s1">'<li><span data-bind="content"/> <button>x</button></li>'</span><span class="p">,</span> <span class="s1">'& span { cursor:pointer; }'</span><span class="p">,</span> <span class="p">{</span>
<span class="s1">'click span'</span><span class="o">:</span> <span class="kd">function</span><span class="p">(){</span>
<span class="kd">var</span> <span class="nx">input</span> <span class="o">=</span> <span class="nx">prompt</span><span class="p">(</span><span class="s1">'Edit to-do item:'</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">model</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="s1">'content'</span><span class="p">));</span>
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">input</span><span class="p">)</span> <span class="k">return</span><span class="p">;</span>
<span class="k">this</span><span class="p">.</span><span class="nx">model</span><span class="p">.</span><span class="nx">set</span><span class="p">({</span><span class="nx">content</span><span class="o">:</span><span class="nx">input</span><span class="p">});</span>
<span class="p">},</span>
<span class="s1">'click button'</span><span class="o">:</span> <span class="kd">function</span><span class="p">(){</span>
<span class="k">this</span><span class="p">.</span><span class="nx">destroy</span><span class="p">();</span>
<span class="p">}</span>
<span class="p">});</span>
<span class="c1">//</span>
<span class="c1">// List of items</span>
<span class="c1">//</span>
<span class="kd">var</span> <span class="nx">list</span> <span class="o">=</span> <span class="nx">$$</span><span class="p">({},</span> <span class="s1">'<div> <button id="new">New item</button> <ul></ul> </div>'</span><span class="p">,</span> <span class="p">{</span>
<span class="s1">'click #new'</span><span class="o">:</span> <span class="kd">function</span><span class="p">(){</span>
<span class="kd">var</span> <span class="nx">newItem</span> <span class="o">=</span> <span class="nx">$$</span><span class="p">(</span><span class="nx">item</span><span class="p">,</span> <span class="p">{</span><span class="nx">content</span><span class="o">:</span><span class="s1">'Click to edit'</span><span class="p">});</span>
<span class="k">this</span><span class="p">.</span><span class="nx">append</span><span class="p">(</span><span class="nx">newItem</span><span class="p">,</span> <span class="s1">'ul'</span><span class="p">);</span> <span class="c1">// add to container, appending at <ul></span>
<span class="p">}</span>
<span class="p">});</span>
<span class="nx">$$</span><span class="p">.</span><span class="nb">document</span><span class="p">.</span><span class="nx">append</span><span class="p">(</span><span class="nx">list</span><span class="p">);</span>
</pre></div>
<p><div class="demo"></div></p>
</div>
</div>
<div id="demo-wrapper" style="display:none"></div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script src="jquery.simplemodal.1.4.1.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
(function($){
//
// Each "Run demo"
//
$('div.demo').each(function(){
var code = $(this).prevAll('.codehilite:first').text();
//
// On demo click
//
var $link = $("<button>Run demo »</button>").click(function(){
var $frame = $('<iframe \/>').attr('src', 'demo.html');
$('div#demo-wrapper')
.hide()
.empty()
.append('<div id="msg">Loading ...</div>')
.append($frame)
.modal({overlayClose: true});
//
// Insert dynamic frame content (pure jQuery doesn't work with script tags)
//
$frame.load(function(){
$('div#demo-wrapper #msg').html('Press <b>esc</b> or click outside box to close')
var frameWindow = this.contentWindow ? this.contentWindow : this.contentDocument.defaultView;
var script = frameWindow.document.createElement("script");
script.type = "text/javascript";
script.text = '\n'+code+'\n';
frameWindow.document.body.appendChild(script);
});
return false;
}); // demo click
$(this).append($link);
}); // each demo
})(jQuery);
</script>
</body>
</html>