-
Notifications
You must be signed in to change notification settings - Fork 0
/
using_handlebars.html
685 lines (607 loc) · 33.1 KB
/
using_handlebars.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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>SproutCore Guides: Using Handlebars</title>
<!-- guides styles -->
<link rel="stylesheet" type="text/css" href="stylesheets/style.css" />
<link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print" />
<link rel="stylesheet" type="text/css" href="stylesheets/guides.css" />
<link rel="stylesheet" type="text/css" href="stylesheets/overrides.style.css" />
<link rel="stylesheet" type="text/css" href="stylesheets/overrides.print.css" media="print" />
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<!-- syntax highlighting styles -->
<link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shCore.css" />
<link rel="stylesheet" type="text/css" href="stylesheets/shThemeGuides.css" />
</head>
<body class="guide">
<header role="banner">
<div class="container">
<h1 id="logo">
<a href="http://www.sproutcore.com"><img src="images/header/logo.png" alt="SproutCore" /></a>
</h1>
<nav role="navigation">
<ul>
<li><a href="http://sproutcore.com/about/">About</a></li>
<li><a href="http://showcase.sproutcore.com">Showcase</a></li>
<li class="active"><a href="http://guides.sproutcore.com">Guides</a></li>
<li><a href="http://docs.sproutcore.com">Docs</a></li>
<li><a href="http://sproutcore.com/community/">Community</a></li>
<li><a href="http://blog.sproutcore.com">Blog</a></li>
</ul>
</nav>
</div>
</header>
<div id="feature">
<div class="wrapper">
<div class="feature_header">
<a href="/"><img src="images/graphics/guides.png"></a>
<h2><a href="/">SproutCore Guides</a></h2>
<p>These guides are designed to help you write and perfect your code.</p>
</div>
<div class="feature_sidebar">
<a href="index.html" id="guidesMenu">
Guides Index <span id="guidesArrow">▸</span>
</a>
<div id="guides" class="clearfix" style="display: none;">
<a href="index.html" class="guidesMenu">
Guides Index <span class="guidesArrow">▾</span>
</a>
<hr style="clear:both;">
<dl class="L">
<dt>Start Here</dt>
<dd><a href="getting_started.html">Getting Started: Part 1</a></dd>
<dd><a href="getting_started_2.html">Getting Started: Part 2</a></dd>
<dd><a href="getting_started_3.html">Getting Started: Part 3</a></dd>
<dd><a href="core_concepts_sc_object.html">Classes and SC.Object</a></dd>
<dd><a href="core_concepts_kvo.html">Computed Properties, Observers and Bindings</a></dd>
<dt>Views</dt>
<dd><a href="views.html">Core View Concepts</a></dd>
<dt>Models</dt>
<dd><a href="records.html">SproutCore Records</a></dd>
<dd><a href="fixtures.html">Using Fixtures</a></dd>
<dt>Theming</dt>
<dd><a href="chance.html">Using Chance, SproutCore's CSS Framework</a></dd>
<dd><a href="theming_app.html">Theming Your App</a></dd>
<dt>Testing</dt>
<dd><a href="unit_test_framework.html">Unit Testing</a></dd>
<dd><a href="adding_unit_test.html">Adding a Unit Test</a></dd>
<dd><a href="writing_unit_tests.html">Writing Unit Tests</a></dd>
<dd><a href="running_unit_tests.html">Running Unit Tests</a></dd>
<dd><a href="todos_tdd.html">SproutCore Development Using TDD</a></dd>
</dl>
<dl class="R">
<dt>Extras</dt>
<dd><a href="build_tools.html">Build Tools</a></dd>
<dd><a href="run_loop.html">The Run Loop</a></dd>
<dd><a href="enumerables.html">Enumerables</a></dd>
<dd><a href="using_handlebars.html">Using Handlebars Templates</a></dd>
<dt>Contributing to SproutCore</dt>
<dd><a href="style_guide.html">Javascript Style Guide</a></dd>
<dd><a href="commit_code.html">Committer Guidelines</a></dd>
<dd><a href="documentation_guidelines.html">Documentation Guidelines</a></dd>
</dl>
</div>
</div>
</div>
</div>
<div id="container">
<div class="wrapper">
<div id="mainCol">
<div class="headerSection">
<h2>Using Handlebars</h2>
<p>This guide covers displaying content in the browser using Handlebars templates and SC.TemplateView. After reading this guide, you will be able to:</p>
<ul>
<li>Describe the markup of your application using Handlebars-flavored <span class="caps">HTML</span>.</li>
<li>Have your <span class="caps">HTML</span> update automatically when your data model changes.</li>
<li>Integrate templates into existing applications.</li>
<li>Build new applications entirely with templates.</li>
</ul>
</div>
<h3 id="handlebars">1 Handlebars</h3>
<p>SproutCore comes bundled with Handlebars, a semantic templating language. These templates look like regular <span class="caps">HTML</span>, with embedded handlebars expressions:</p>
<div class="code_container">
<pre class="brush: xml; gutter: false; toolbar: false">
Hello, <b>{{name}}</b>!
</pre></div><p>You should store your Handlebars templates inside your application’s <tt>resources/templates</tt> directory. Make sure you save them with a <tt>.handlebars</tt> extension. At runtime, SproutCore will compile and load these templates so they are available for you to use in your views.</p>
<h3 id="sc-templateview">2 SC.TemplateView</h3>
<p>An SC.TemplateView is a view that is responsible for rendering a Handlebars template and inserting it into the <span class="caps">DOM</span>.</p>
<p>To tell the TemplateView which template to use, set its <tt>templateName</tt> property. For example, if I saved a template to a file called <tt>sayhello.handlebars</tt>, I would set the <tt>templateName</tt> property to <tt>"sayhello"</tt>.</p>
<div class="code_container">
<div class="filename">apps/myapp/resources/templates/sayhello.handlebars</div>
<pre class="brush: xml; gutter: false; toolbar: false">
Hello, <b>{{name}}</b>!
</pre></div>
<div class="code_container">
<pre class="brush: javascript; gutter: false; toolbar: false">
var view = SC.TemplateView.create({
templateName: 'sayhello',
name: "Bob"
});
</pre></div>
<div class="note"><p>For the remainder of the guide, the <ins>templateName</ins> property will be omitted from most examples. You can assume that if we show a code sample that includes an SC.TemplateView and a Handlebars template, the view has been configured to display that template via the <ins>templateName</ins> property.</p></div>
<h3 id="handlebars-basics">3 Handlebars Basics</h3>
<p>As you’ve already seen, you can print the value of a property by enclosing it in a Handlebars expression, or a series of braces, like this:</p>
<div class="code_container">
<pre class="brush: xml; gutter: false; toolbar: false">
My new car is {{color}}.
</pre></div><p>This will look up and print the TemplateView’s <tt>color</tt> property. For example, if your view looks like this:</p>
<div class="code_container">
<pre class="brush: javascript; gutter: false; toolbar: false">
App.CarView = SC.TemplateView.extend({
color: 'blue'
});
</pre></div><p>Your view would appear in the browser like this:</p>
<div class="code_container">
<pre class="brush: xml; gutter: false; toolbar: false">
My new car is blue.
</pre></div><p>All of the features described in this guide are <i>bindings aware</i>. That means that if the values used by your templates ever change, your <span class="caps">HTML</span> will be updated automatically. It’s like magic.</p>
<p>In order to know which part of your <span class="caps">HTML</span> to update when an underlying property changes, Handlebars will insert marker elements with a unique ID. If you look at your application while it’s running, you might notice these extra elements:</p>
<div class="code_container">
<pre class="brush: xml; gutter: false; toolbar: false">
My new car is <span id="sc232">blue</span>.
</pre></div><p>Because all Handlebars expressions are wrapped in these markers, make sure each <span class="caps">HTML</span> tag stays inside the same block. For example, you shouldn’t do this:</p>
<div class="code_container">
<pre class="brush: xml; gutter: false; toolbar: false">
<!-- Don't do it! -->
<div {{#if isUrgent}}class="urgent"{{/if}}>
</pre></div><h4 id="if-else-and-unless">3.1 {{#if}}, {{else}}, and {{#unless}}</h4>
<p>Sometimes you may only want to display part of your template if a property exists. For example, let’s say we have a view with a <tt>person</tt> property that contains an object with <tt>firstName</tt> and <tt>lastName</tt> fields:</p>
<div class="code_container">
<pre class="brush: javascript; gutter: false; toolbar: false">
App.SayHelloView = SC.TemplateView.extend({
person: SC.Object.create({
firstName: "Joy",
lastName: "Clojure"
})
});
</pre></div><p>In order to display part of the template only if the <tt>person</tt> object exists, we can use the <tt>{{#if}}</tt> helper to conditionally render a block:</p>
<div class="code_container">
<pre class="brush: xml; gutter: false; toolbar: false">
{{#if person}}
Welcome back, <b>{{person.firstName}} {{person.lastName}}</b>!
{{/if}}
</pre></div>
<div class="note"><p>Handlebars will not render the block if the argument passed evaluates to <ins>false</ins>, <ins>undefined</ins>, <ins>null</ins> or <ins>[]</ins> (i.e., any “falsy” value).</p></div>
<p>If the expression evaluates to falsy, we can also display an alternate template using <tt>{{else}}</tt>:</p>
<div class="code_container">
<pre class="brush: xml; gutter: false; toolbar: false">
{{#if person}}
Welcome back, <b>{{person.firstName}} {{person.lastName}}</b>!
{{else}}
Please log in.
{{/if}}
</pre></div><p>To only render a block if a value is falsy, use <tt>{{#unless}}</tt>:</p>
<div class="code_container">
<pre class="brush: xml; gutter: false; toolbar: false">
{{#unless hasPaid}}
You owe: ${{total}}
{{/unless}}
</pre></div><p><tt>{{#if}}</tt> and <tt>{{#unless}}</tt> are examples of block expressions. These allow you to invoke a helper with a portion of your template. Block expressions look like normal expressions except that they contain a hash (#) before the helper name, and require a closing expression.</p>
<h4 id="with">3.2 {{#with}}</h4>
<p>Sometimes you may want to invoke a section of your template with a context different than the SC.TemplateView. For example, we can clean up the above template by using the <tt>{{#with}}</tt> helper:</p>
<div class="code_container">
<pre class="brush: xml; gutter: false; toolbar: false">
{{#with person}}
Welcome back, <b>{{firstName}} {{lastName}}</b>!
{{/with}}
</pre></div>
<div class="note"><p>{{#with}} changes the <em>context</em> of the block you pass to it. The context is the object on which properties are looked up. By default, the context is the SC.TemplateView to which the template belongs.</p></div>
<h4 id="binding-element-attributes-with-bindattr">3.3 Binding Element Attributes with {{bindAttr}}</h4>
<p>In addition to text, you may also want your templates to dictate the attributes of your <span class="caps">HTML</span> elements. For example, imagine a view that contains a <span class="caps">URL</span>:</p>
<div class="code_container">
<pre class="brush: javascript; gutter: false; toolbar: false">
App.LogoView = SC.TemplateView.extend({
logoUrl: 'http://www.mycorp.com/images/logo.png'
});
</pre></div><p>The best way to display the <span class="caps">URL</span> as an image in Handlebars is like this:</p>
<div class="code_container">
<pre class="brush: xml; gutter: false; toolbar: false">
<div id="logo">
<img {{bindAttr src="logoUrl"}} alt="Logo">
</div>
</pre></div><p>This generates the following <span class="caps">HTML</span>:</p>
<div class="code_container">
<pre class="brush: xml; gutter: false; toolbar: false">
<div id="logo">
<img src="http://www.mycorp.com/images/logo.png" alt="Logo">
</div>
</pre></div><p>If you use <tt>{{bindAttr}}</tt> with a Boolean value, it will add or remove the specified attribute. For example, given this SproutCore view:</p>
<div class="code_container">
<pre class="brush: javascript; gutter: false; toolbar: false">
App.InputView = SC.TemplateView.extend({
isSelected: true
});
</pre></div><p>And this template:</p>
<div class="code_container">
<pre class="brush: xml; gutter: false; toolbar: false">
<input type="checkbox" {{bindAttr checked="isSelected"}}>
</pre></div><p>Handlebars will produce the following <span class="caps">HTML</span> element:</p>
<div class="code_container">
<pre class="brush: xml; gutter: false; toolbar: false">
<input type="checkbox" checked>
</pre></div><h4 id="binding-class-names-with-bindattr">3.4 Binding Class Names with {{bindAttr}}</h4>
<p>The <tt>class</tt> attribute can be bound like any other attribute, but it also has some additional special behavior. The default behavior works like you’d expect:</p>
<div class="code_container">
<pre class="brush: javascript; gutter: false; toolbar: false">
App.AlertView = SC.TemplateView.extend({
priority: "p4",
isUrgent: true
});
</pre></div>
<div class="code_container">
<pre class="brush: xml; gutter: false; toolbar: false">
<div {{bindAttr class="priority"}}>
Warning!
</div>
</pre></div><p>This template will emit the following <span class="caps">HTML</span>:</p>
<div class="code_container">
<pre class="brush: xml; gutter: false; toolbar: false">
<div class="p4">
Warning!
</div>
</pre></div><p>If the value to which you bind is a Boolean, however, the dasherized version of that property will be applied as a class:</p>
<div class="code_container">
<pre class="brush: xml; gutter: false; toolbar: false">
<div {{bindAttr class="isUrgent"}}>
Warning!
</div>
</pre></div><p>This emits the following <span class="caps">HTML</span>:</p>
<div class="code_container">
<pre class="brush: xml; gutter: false; toolbar: false">
<div class="is-urgent">
Warning!
</div>
</pre></div><p>Unlike other attributes, you can also bind multiple classes:</p>
<div class="code_container">
<pre class="brush: xml; gutter: false; toolbar: false">
<div {{bindAttr class="isUrgent priority"}}>
Warning!
</div>
</pre></div><h4 id="localized-strings-with-loc">3.5 Localized Strings with {{loc}}</h4>
<p>SproutCore has built-in support for localized applications. To emit a localized version of a string, use the <tt>{{loc}}</tt> helper:</p>
<div class="code_container">
<pre class="brush: xml; gutter: false; toolbar: false">
{{loc myLocalizedString}}
</pre></div><h3 id="building-a-view-hierarchy">4 Building a View Hierarchy</h3>
<p>So far, we’ve discussed writing templates for a single view. However, as your application grows, you will often want to create a hierarchy of views to encapsulate different areas on the page. Each view is responsible for handling events and maintaining the properties needed to display it.</p>
<h4 id="view">4.1 {{view}}</h4>
<p>To add a child view to a parent, use the <tt>{{view}}</tt> helper, which takes a path to a view class.</p>
<div class="code_container">
<pre class="brush: javascript; gutter: false; toolbar: false">
// Define parent view
App.UserView = SC.TemplateView.extend({
templateName: 'user',
firstName: "Albert",
lastName: "Hofmann"
});
// Define child view
App.InfoView = SC.TemplateView.extend({
templateName: 'info',
posts: 25,
hobbies: "Riding bicycles"
});
</pre></div>
<div class="code_container">
<div class="filename">user.handlebars</div>
<pre class="brush: xml; gutter: false; toolbar: false">
User: {{firstName}} {{lastName}}
{{view App.InfoView}}
</pre></div>
<div class="code_container">
<div class="filename">info.handlebars</div>
<pre class="brush: xml; gutter: false; toolbar: false">
<b>Posts:</b> {{posts}}
<br>
<b>Hobbies:</b> {{hobbies}}
</pre></div><p>If we were to create an instance of <code>App.UserView</code> and render it, we would get a <span class="caps">DOM</span> representation like this:</p>
<div class="code_container">
<pre class="brush: xml; gutter: false; toolbar: false">
User: Albert Hofmann
<div>
<b>Posts:</b> 25
<br>
<b>Hobbies:</b> Riding bicycles
</div>
</pre></div><h4 id="relative-paths">4.2 Relative Paths</h4>
<p>Instead of specifying an absolute path, you can also specify which view class to use relative to the parent view. For example, we could nest the above view hierarchy like this:</p>
<div class="code_container">
<pre class="brush: javascript; gutter: false; toolbar: false">
App.UserView = SC.TemplateView.extend({
templateName: 'user',
firstName: "Albert",
lastName: "Hofmann",
InfoView: SC.TemplateView.extend({
templateName: 'info',
posts: 25,
hobbies: "Riding bicycles"
})
});
</pre></div>
<div class="code_container">
<div class="filename">user.handlebars</div>
<pre class="brush: xml; gutter: false; toolbar: false">
User: {{firstName}} {{lastName}}
{{view InfoView}}
</pre></div><h4 id="setting-child-view-templates">4.3 Setting Child View Templates</h4>
<p>If you’d like to specify the template your child views use (instead of having to place them in a separate Handlebars file), you can use the block form of the <tt>{{view}}</tt> helper. We might rewrite the above example like this:</p>
<div class="code_container">
<pre class="brush: javascript; gutter: false; toolbar: false">
App.UserView = SC.TemplateView.extend({
templateName: 'user',
firstName: "Albert",
lastName: "Hofmann"
});
App.InfoView = SC.TemplateView.extend({
posts: 25,
hobbies: "Riding bicycles"
});
</pre></div>
<div class="code_container">
<div class="filename">user.handlebars</div>
<pre class="brush: xml; gutter: false; toolbar: false">
User: {{firstName}} {{lastName}}
{{#view App.InfoView}}
<b>Posts:</b> {{posts}}
<br>
<b>Hobbies:</b> {{hobbies}}
{{/view}}
</pre></div><p>When you do this, it may be helpful to think of it as assigning views to portions of the page. This allows you to encapsulate event handling for just that part of the page.</p>
<h4 id="setting-up-bindings">4.4 Setting Up Bindings</h4>
<p>So far in our examples, we have been setting static values directly on the views. But to best implement an <span class="caps">MVC</span> architecture, we should actually be binding the properties of our views to the controller layer.</p>
<p>Let’s set up a controller to represent our user data:</p>
<div class="code_container">
<pre class="brush: javascript; gutter: false; toolbar: false">
App.userController = SC.ObjectController.create({
content: SC.Object.create({
firstName: "Albert",
lastName: "Hofmann",
posts: 25,
hobbies: "Riding bicycles"
})
});
</pre></div>
<div class="info"><p>An <code>SC.ObjectController</code> is a controller that serves as a proxy to another object, contained in the controller’s <ins>content</ins> property. If you want to change the object that your views are displaying, just set the <ins>content</ins> property to the new object. Since your views are all bound to the controller, they will update automatically without having to reconfigure their bindings.</p></div>
<p>Now let’s update <code>App.UserView</code> to bind to <code>App.userController</code>:</p>
<div class="code_container">
<pre class="brush: javascript; gutter: false; toolbar: false">
App.UserView = SC.TemplateView.extend({
templateName: 'user',
firstNameBinding: 'App.userController.firstName',
lastNameBinding: 'App.userController.lastName'
});
</pre></div><p>When we only have a few bindings to configure, like with <code>App.InfoView</code>, it is sometimes useful to be able to declare those bindings in the template. You can do that by passing additional arguments to the <tt>{{#view}}</tt> helper. If all you’re doing is configuring bindings, this often allows you to bypass having to create a new subclass.</p>
<div class="code_container">
<div class="filename">user.handlebars</div>
<pre class="brush: xml; gutter: false; toolbar: false">
User: {{firstName}} {{lastName}}
{{#view App.InfoView postsBinding="App.userController.posts"
hobbiesBinding="App.userController.hobbies"}}
<b>Posts:</b> {{posts}}
<br>
<b>Hobbies:</b> {{hobbies}}
{{/view}}
</pre></div>
<div class="note"><p>You can actually pass <i>any</i> property as a parameter to {{view}}, not just bindings. However, if you are doing anything other than setting up bindings, it is generally a good idea to create a new subclass.</p></div>
<h4 id="modifying-a-view-s-html">4.5 Modifying a View’s <span class="caps">HTML</span></h4>
<p>When you append a view, it creates a new <span class="caps">HTML</span> element that holds its content. If your view has any child views, they will also be displayed as child nodes of the parent’s <span class="caps">HTML</span> element.</p>
<p>By default, new instances of <code>SC.View</code> create a <tt><div></tt> element. You can override this by passing a <tt>tagName</tt> parameter:</p>
<div class="code_container">
<pre class="brush: xml; gutter: false; toolbar: false">
{{view App.InfoView tagName="span"}}
</pre></div><p>You can also assign an ID attribute to the view’s <span class="caps">HTML</span> element by passing an <tt>id</tt> parameter:</p>
<div class="code_container">
<pre class="brush: xml; gutter: false; toolbar: false">
{{view App.InfoView id="info-view"}}
</pre></div><p>This makes it easy to style using <span class="caps">CSS</span> ID selectors:</p>
<div class="code_container">
<pre class="brush: css; gutter: false; toolbar: false">
/** Give the view a red background. **/
#info-view {
background-color: red;
}
</pre></div><p>You can assign class names similarly:</p>
<div class="code_container">
<pre class="brush: xml; gutter: false; toolbar: false">
{{view App.InfoView class="info urgent"}}
</pre></div><p>You can bind class names to a property of the view by using <tt>classBinding</tt> instead of <tt>class</tt>. The same behavior as described in <tt>bindAttr</tt> applies:</p>
<div class="code_container">
<pre class="brush: javascript; gutter: false; toolbar: false">
App.AlertView = SC.TemplateView.extend({
priority: "p4",
isUrgent: true
});
</pre></div>
<div class="code_container">
<pre class="brush: xml; gutter: false; toolbar: false">
{{view App.AlertView classBinding="isUrgent priority"}}
</pre></div><p>This yields a view wrapper that will look something like this:</p>
<div class="code_container">
<pre class="brush: xml; gutter: false; toolbar: false">
<div id="sc420" class="sc-view is-urgent p4"></div>
</pre></div><h3 id="displaying-a-list-of-items">5 Displaying a List of Items</h3>
<p>If you need to display a basic list of items, use Handlebar’s <tt>{{#each}}</tt> helper:</p>
<div class="code_container">
<pre class="brush: javascript; gutter: false; toolbar: false">
App.PeopleView = SC.TemplateView.extend({
people: [ SC.Object.create({ name: 'Steph' }),
SC.Object.create({ name: 'Tom' }) ]
});
</pre></div>
<div class="code_container">
<pre class="brush: xml; gutter: false; toolbar: false">
{{#each people}}
Hello, {{name}}!
{{/each}}
</pre></div><p>This will print a list like this:</p>
<div class="code_container">
<pre class="brush: xml; gutter: false; toolbar: false">
<ul>
<li>Hello, Steph!</li>
<li>Hello, Tom!</li>
</ul>
</pre></div><h4 id="sc-templatecollectionview">5.1 SC.TemplateCollectionView</h4>
<p>Sometimes you will need each item in your list to handle events. In that case, you will need more sophistication than what <tt>{{#each}}</tt> can provide. You can use the <tt>{{#collection}}</tt> helper to create a new <code>SC.TemplateCollectionView</code>. You can bind the instance of <code>SC.TemplateCollectionView</code> to an array, and it will create a new <code>SC.TemplateView</code> for each item.</p>
<p>Usually, you will bind the collection to an <code>SC.ArrayController</code>, like this:</p>
<div class="code_container">
<pre class="brush: javascript; gutter: false; toolbar: false">
App.peopleController = SC.ArrayController.create({
content: ['Steph', 'Tom', 'Ryan', 'Chris', 'Jill']
});
</pre></div>
<div class="code_container">
<pre class="brush: xml; gutter: false; toolbar: false">
{{#collection SC.TemplateCollectionView contentBinding="App.peopleController"}}
Hi, {{content}}!
{{/collection}}
</pre></div>
<div class="note"><p>The template you pass to the block helper will look up properties relative to each child view. To access the item in the array that the view should represent, refer to the <ins>content</ins> property via {{content}}.</p></div>
<p>Remember that under the hood, <code>SC.TemplateCollectionView</code> creates a new view for each item in the bound array. By default, each new view will be an instance of <code>SC.TemplateView</code>. You can tell the collection which type of view to create instances of by subclassing <code>SC.TemplateCollectionView</code> and supplying a custom class:</p>
<div class="code_container">
<pre class="brush: javascript; gutter: false; toolbar: false">
App.PeopleCollectionView = SC.TemplateCollectionView.extend({
itemView: SC.TemplateView.extend({
mouseDown: function(evt) {
window.alert('You clicked on ' + this.get('content'));
}
})
});
</pre></div>
<div class="code_container">
<pre class="brush: xml; gutter: false; toolbar: false">
{{#collection App.PeopleCollectionView contentBinding="App.peopleController"}}
Hi, {{content}}!
{{/collection}}
</pre></div><p>If you run this code, you should see an alert every time you click on one of the items.</p>
<p>The <tt>{{#collection}}</tt> helper takes the same options as <tt>{{#view}}</tt>, as described above. For example, you can set an <span class="caps">HTML</span> <tt>id</tt> attribute on the container of <code>SC.TemplateCollectionView</code> like this:</p>
<div class="code_container">
<pre class="brush: xml; gutter: false; toolbar: false">
{{collection App.MyCollectionView id="my-collection"}}
</pre></div><p>What if you want to set the class name of every child view, though? If you prepend an option with <tt>item</tt>, that option will instead be set on the child. For example, let’s say you wanted to set a class name on each item in your collection:</p>
<div class="code_container">
<pre class="brush: xml; gutter: false; toolbar: false">
{{collection App.MyCollectionView itemClass="collection-item"}}
</pre></div><h3 id="using-sc-templateview-inside-desktop-controls">6 Using SC.TemplateView Inside Desktop Controls</h3>
<p>If you’re building a desktop-style application, you can get the best of both native and web worlds by integrating <code>SC.TemplateViews</code> into SproutCore’s other controls. For example, you can easily embed an <code>SC.TemplateView</code> inside an <code>SC.ScrollView</code>:</p>
<div class="code_container">
<div class="filename">apps/app/resources/main_page.js</div>
<pre class="brush: javascript; gutter: false; toolbar: false">
App.mainPage = SC.Page.design({
mainPane: SC.MainPane.extend({
childViews: ['scrollView'],
scrollView: SC.ScrollView.design({
contentView: SC.TemplateView.design({
templateName: 'main_page'
})
})
})
});
</pre></div><p><!-- h3. Under the Hood: Template Functions --></p>
<h3 id="changelog">7 Changelog</h3>
<ul>
<li>May 5, 2011: Initial version by <a href="credits.html#tomdale">Tom Dale</a></li>
</ul>
</div>
<div id="subCol">
<h3 class="chapter"><img src="images/graphics/chapters.png" alt="" />Chapters</h3>
<ol class="chapters">
<li><a href="#handlebars"><p>Handlebars</p>
</a></li><li><a href="#sc-templateview"><p>SC.TemplateView</p>
</a></li><li><a href="#handlebars-basics"><p>Handlebars Basics</p>
</a><ul><li><a href="#if-else-and-unless"><p>{{#if}}, {{else}}, and {{#unless}}</p>
</a></li> <li><a href="#with"><p>{{#with}}</p>
</a></li> <li><a href="#binding-element-attributes-with-bindattr"><p>Binding Element Attributes with {{bindAttr}}</p>
</a></li> <li><a href="#binding-class-names-with-bindattr"><p>Binding Class Names with {{bindAttr}}</p>
</a></li> <li><a href="#localized-strings-with-loc"><p>Localized Strings with {{loc}}</p>
</a></li></ul></li><li><a href="#building-a-view-hierarchy"><p>Building a View Hierarchy</p>
</a><ul><li><a href="#view"><p>{{view}}</p>
</a></li> <li><a href="#relative-paths"><p>Relative Paths</p>
</a></li> <li><a href="#setting-child-view-templates"><p>Setting Child View Templates</p>
</a></li> <li><a href="#setting-up-bindings"><p>Setting Up Bindings</p>
</a></li> <li><a href="#modifying-a-view-s-html"><p>Modifying a View’s <span class="caps">HTML</span></p>
</a></li></ul></li><li><a href="#displaying-a-list-of-items"><p>Displaying a List of Items</p>
</a><ul><li><a href="#sc-templatecollectionview"><p>SC.TemplateCollectionView</p>
</a></li></ul></li><li><a href="#using-sc-templateview-inside-desktop-controls"><p>Using SC.TemplateView Inside Desktop Controls</p>
</a></li><li><a href="#changelog"><p>Changelog</p>
</a></li>
</ol>
</div>
</div>
</div>
<hr class="hide" />
<footer>
<div class="container">
<div class="col">
<a href="index.html"><img src="images/footer/sc_logo_medium.png" alt="SproutCore" /></a>
<p id="license">
SproutCore framework is under MIT License.
</p>
</div>
<nav class="col">
<h1>Site Navigation</h1>
<ul>
<li><a href="http://www.sproutcore.com/about/">About</a></li>
<li><a href="http://docs.sproutcore.com">Docs</a></li>
<li><a href="http://blog.sproutcore.com">Blog</a></li>
<li><a href="http://www.sproutcore.com/install/?redirect=1">Download</a></li>
<li><a href="http://www.sproutcore.com/community/">Community</a></li>
<li><a href="https://github.com/sproutcore/sproutcore">GitHub</a></li>
<li><a href="mailto:community@sproutcore.com">Contact Us</a></li>
<li><a href="http://guides.sproutcore.com">Guides</a></li>
</ul>
<a href="http://www.w3.org/html/logo/"><img src="images/footer/html5_tech.png" alt="HTML5 Powered" /></a>
</nav>
<div class="col">
<h1>Get in Touch, Stay Informed</h1>
<ul class="social">
<li><a href="http://twitter.com/sproutcore"><img src="images/footer/twitter.png" alt="Twitter" /></a></li>
<li><a href="http://www.facebook.com/sproutcore"><img src="images/footer/facebook.png" alt="Facebook" /></a></li>
<li><a href="http://groups.google.com/group/sproutcore"><img src="images/footer/google.png" alt="Google Groups" /></a></li>
<li><a href="http://groups.google.com/group/sproutcore">Subscribe To Mailing List</a></li>
</ul>
<a href="http://eepurl.com/dK1-Y" target="_blank" id="newsletter" class="button secondary">Sign Up For Our Newsletter!</a>
<!-- Commented out until we have mailchimp API integration.
<h2>Subscribe To Newsletter</h2>
<div id="subscribe">
<form>
<input type="text" name="email" />
<button name="subscribe">Sign-Up</button>
<div class="processing"><img src="/img/spinner.gif"></div>
<div class="error"></div>
</form>
</div>
-->
</div>
<a href="#feature" class="top">Back To Top</a>
</div>
</footer>
<script src="http://code.jquery.com/jquery-1.6.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" src="javascripts/sproutGuides.js"></script>
<script type="text/javascript" src="javascripts/syntaxhighlighter/shCore.js"></script>
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushRuby.js"></script>
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushJScript.js"></script>
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushCss.js"></script>
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushXml.js"></script>
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushSql.js"></script>
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushPlain.js"></script>
<script type="text/javascript">
SyntaxHighlighter.all()
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-19584397-1']);
_gaq.push(['_setDomainName', '.sproutcore.com']);
_gaq.push(['_setAllowHash', false]);
_gaq.push(['_setAllowLinker', true]);
_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>
</body>
</html>