/
index.html
699 lines (544 loc) · 31.4 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
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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="Description" content="Ember.js的中文开发者交流平台,提供Ember.js官方网站指南和API的中文翻译,持续关注最近的社区动态,为您提供高质量的Ember.js中文资料。 " />
<meta name="Keywords" content="Ember.js、Ember、Javascript、Javascript框架、HTML、论坛、博客、中文社区" />
<title>Ember.js - 包裹内容</title>
<link rel="shortcut icon" href="/images/favicon.png" />
<!--[if lte IE 7 ]><link href="/stylesheets/fonts/fontello-ie7.css" media="screen" rel="stylesheet" type="text/css" /><![endif]-->
<link href="/stylesheets/site.css" media="screen" rel="stylesheet" type="text/css" />
<link href="/stylesheets/highlight.css" media="screen" rel="stylesheet" type="text/css" />
<link href="/stylesheets/emberjs-cn.css" media="screen" rel="stylesheet" type="text/css" />
<link href="http://emberjs.cn/blog/feed.xml" rel="alternate" type="application/atom+xml" title="Ember.js CN - 博客" />
<script type="text/javascript">
var _gaq = _gaq || [];
var pluginUrl = '//www.google-analytics.com/plugins/ga/inpage_linkid.js';
_gaq.push(['_require', 'inpage_linkid', pluginUrl]);
_gaq.push(['_setAccount', 'UA-38578402-1']);
_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>
<!--[if lt IE 9]>
<script type="text/javascript" src="/javascripts/common-old-ie.js"></script>
<![endif]-->
<!--[if gte IE 9]><!-->
<script type="text/javascript" src="/javascripts/common-modern.js"></script>
<!--<![endif]-->
<script type="text/javascript" src="/javascripts/guides.js"></script>
</head>
<body class="bilingual_guides bilingual_guides_components bilingual_guides_components_wrapping-content-in-a-component bilingual_guides_components_wrapping-content-in-a-component_index">
<div id="header">
<div id="wrapper">
<a id="logo" href="/"> </a>
<ul id="nav">
<li><a href="/guides">指南</a></li>
<li><a href="/blog">博客</a></li>
<li>
<a target="_blank" href="http://discuss.emberjs.cn">论坛</a>
</li>
<li><a href="/builds">构建</a></li>
</ul>
<div id="github">
<a href="https://github.com/emberjs-cn"><img src="/images/fork-us.png">Fork Us!</a>
<a href="https://github.com/emberjs/ember.js"><img src="/images/fork-us.png">Fork Ember!</a>
</div>
</div>
</div>
<div id="content-wrapper">
<div id="sidebar">
<ol id="toc-list">
<li class="level-1">
<a href="/bilingual_guides/getting-started">入门指南</a>
<ol>
<li class="level-3">
<a href="/bilingual_guides/getting-started">介绍</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/getting-started/planning-the-application">应用规划</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/getting-started/creating-a-static-mockup">创建静态页面</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/getting-started/obtaining-emberjs-and-dependencies">获取Ember.js和相应依赖</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/getting-started/adding-a-route-and-template">添加第一个路由与模板</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/getting-started/modeling-data">建立模型</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/getting-started/using-fixtures">使用静态数据</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/getting-started/displaying-model-data">显示模型数据</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/getting-started/displaying-a-models-completeness">显示模型的完成状态</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/getting-started/creating-a-new-model">创建新的模型实例</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/getting-started/marking-a-model-as-complete-incomplete">标记模型为完成或未完成</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/getting-started/displaying-the-number-of-incomplete-todos">显示未完成待办事项的数量</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/getting-started/toggle-todo-editing-state">切换显示和编辑状态</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/getting-started/accepting-edits">接受修改</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/getting-started/deleting-todos">删除模型</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/getting-started/adding-child-routes">添加子路由</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/getting-started/show-only-incomplete-todos">显示未完成待办事项</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/getting-started/show-only-complete-todos">显示已完成待办事项</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/getting-started/show-all-todos-again">显示所有待办事项</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/getting-started/display-a-button-to-remove-completed-todos">添加移除所有已完成待办事项的按钮</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/getting-started/show-when-all-todos-are-complete">提示所有待办事项已完成</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/getting-started/toggle-all-todos">切换已完成和未完成待办事项</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/getting-started/using-other-adapters">更换FixtureAdapter</a>
</li>
</ol></li>
<li class="level-1">
<a href="/bilingual_guides/concepts/core-concepts">概念</a>
<ol>
<li class="level-3">
<a href="/bilingual_guides/concepts/core-concepts">核心概念</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/concepts/naming-conventions">命名惯例</a>
</li>
</ol></li>
<li class="level-1">
<a href="/bilingual_guides/object-model/classes-and-instances">对象模型</a>
<ol>
<li class="level-3">
<a href="/bilingual_guides/object-model/classes-and-instances">类与实例</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/object-model/computed-properties">计算属性</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/object-model/computed-properties-and-aggregate-data">计算属性和带@each的集合数据</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/object-model/observers">观察器</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/object-model/bindings">绑定</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/object-model/reopening-classes-and-instances">重新打开类和实例</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/object-model/what-do-i-use-when">绑定,观察器,计算属性:如何选择?</a>
</li>
</ol></li>
<li class="level-1">
<a href="/bilingual_guides/application">应用</a>
<ol>
<li class="level-3">
<a href="/bilingual_guides/application">介绍</a>
</li>
</ol></li>
<li class="level-1">
<a href="/bilingual_guides/templates/the-application-template">模板</a>
<ol>
<li class="level-3">
<a href="/bilingual_guides/templates/the-application-template">应用模板</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/templates/handlebars-basics">Handlebars基础</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/templates/conditionals">条件表达式</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/templates/displaying-a-list-of-items">显示项目列表</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/templates/changing-scope">切换作用域</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/templates/binding-element-attributes">绑定元素属性</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/templates/binding-element-class-names">绑定元素类名称</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/templates/links">链接</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/templates/actions">操作</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/templates/input-helpers">Input Helpers</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/templates/rendering-with-helpers">用助手来渲染</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/templates/writing-helpers">编写助手方法</a>
</li>
</ol></li>
<li class="level-1">
<a href="/bilingual_guides/routing">路由</a>
<ol>
<li class="level-3">
<a href="/bilingual_guides/routing">介绍</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/routing/defining-your-routes">定义路由</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/routing/generated-objects">生成的对象</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/routing/specifying-a-routes-model">指定路由的模型</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/routing/setting-up-a-controller">设置控制器</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/routing/rendering-a-template">渲染模板</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/routing/redirection">重定向</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/routing/specifying-the-location-api">指定地址API</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/routing/query-params">Query Parameters</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/routing/asynchronous-routing">Asynchronous Routing</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/routing/loading-and-error-substates">Loading / Error Substates</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/routing/preventing-and-retrying-transitions">Preventing and Retrying Transitions</a>
</li>
</ol></li>
<li class="level-1 selected">
<a href="/bilingual_guides/components">组件</a>
<ol class='selected'>
<li class="level-3">
<a href="/bilingual_guides/components">介绍</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/components/defining-a-component">定义组件</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/components/passing-properties-to-a-component">传递属性</a>
</li>
<li class="level-3 sub-selected">
<a href="/bilingual_guides/components/wrapping-content-in-a-component">包裹内容</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/components/customizing-a-components-element">自定义组件元素</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/components/handling-user-interaction-with-actions">使用Action处理用户交互</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/components/sending-actions-from-components-to-your-application">Sending Actions from Components to Your Application</a>
</li>
</ol></li>
<li class="level-1">
<a href="/bilingual_guides/controllers">控制器</a>
<ol>
<li class="level-3">
<a href="/bilingual_guides/controllers">介绍</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/controllers/representing-a-single-model-with-objectcontroller">代表单一模型</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/controllers/representing-multiple-models-with-arraycontroller">代表多模型</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/controllers/dependencies-between-controllers">管理控制器间的依赖</a>
</li>
</ol></li>
<li class="level-1">
<a href="/bilingual_guides/models">模型</a>
<ol>
<li class="level-3">
<a href="/bilingual_guides/models">介绍</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/models/defining-models">定义模型</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/models/creating-and-deleting-records">创建和删除记录</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/models/pushing-records-into-the-store">将记录推入仓库</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/models/persisting-records">持久化记录</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/models/finding-records">查询记录</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/models/working-with-records">使用记录</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/models/connecting-to-an-http-server">连接HTTP服务器</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/models/handling-metadata">处理元数据</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/models/frequently-asked-questions">常见问题</a>
</li>
</ol></li>
<li class="level-1">
<a href="/bilingual_guides/views">视图</a>
<ol>
<li class="level-3">
<a href="/bilingual_guides/views">介绍</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/views/defining-a-view">定义视图</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/views/handling-events">处理事件</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/views/inserting-views-in-templates">在模板中插入视图</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/views/adding-layouts-to-views">为视图添加布局</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/views/customizing-a-views-element">自定义视图元素</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/views/built-in-views">内置视图</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/views/manually-managing-view-hierarchy">手动管理视图层级</a>
</li>
</ol></li>
<li class="level-1">
<a href="/bilingual_guides/enumerables">枚举</a>
<ol>
<li class="level-3">
<a href="/bilingual_guides/enumerables">介绍</a>
</li>
</ol></li>
<li class="level-1">
<a href="/bilingual_guides/testing/integration">测试</a>
<ol>
<li class="level-3">
<a href="/bilingual_guides/testing/integration">集成测试</a>
</li>
</ol></li>
<li class="level-1">
<a href="/bilingual_guides/configuring-ember/disabling-prototype-extensions">配置Ember.js</a>
<ol>
<li class="level-3">
<a href="/bilingual_guides/configuring-ember/disabling-prototype-extensions">禁用基本类型扩展</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/configuring-ember/embedding-applications">嵌入式应用</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/configuring-ember/feature-flags">特性标识</a>
</li>
</ol></li>
<li class="level-1">
<a href="/bilingual_guides/cookbook">Cookbook</a>
<ol>
<li class="level-3">
<a href="/bilingual_guides/cookbook">简介</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/cookbook/user_interface_and_interaction">用户界面与交互</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/cookbook/event_handling_and_data_binding">事件处理和数据绑定</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/cookbook/helpers_and_components">助手与组件</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/cookbook/working_with_objects">使用对象</a>
</li>
</ol></li>
<li class="level-1">
<a href="/bilingual_guides/understanding-ember/the-view-layer">理解Ember.js</a>
<ol>
<li class="level-3">
<a href="/bilingual_guides/understanding-ember/the-view-layer">视图层</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/understanding-ember/managing-asynchrony">管理异步</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/understanding-ember/keeping-templates-up-to-date">模板自动更新</a>
</li>
<li class="level-3">
<a href="/bilingual_guides/understanding-ember/debugging">调试</a>
</li>
</ol></li></ol>
<div id="back-to-top"><a id="back-top-top" href="#">⬆ 返回顶部</a></div>
</div>
<div id="content" class="has-sidebar">
<div class="chapter">
<h1>包裹内容
<a href="https://github.com/emberjs-cn/www.emberjs.cn/tree/master/source/guides/components/wrapping-content-in-a-component.md" target="_blank" class="edit-page">
编辑页面</a>
</h1>
<p>英文原文:<a href="http://emberjs.com/guides/components/wrapping-content-in-a-component/">http://emberjs.com/guides/components/wrapping-content-in-a-component/</a></p>
<p>中英对照:<a href="http://emberjs.cn/bilingual_guides/components/wrapping-content-in-a-component/">http://emberjs.cn/bilingual_guides/components/wrapping-content-in-a-component/</a></p>
<p>Sometimes, you may want to define a component that wraps content
provided by other templates.</p>
<p>有时候,可能需要定义一个组件来包裹其他模板提供的内容。</p>
<p>For example, imagine we are building a <code>blog-post</code> component that we can
use in our application to display a blog post:</p>
<p>例如,假设正在创建一个用于显示一篇博客的<code>blog-post</code>组件:</p>
<div class="highlight handlebars "><div class="ribbon"></div><div class="scroller"><table class="CodeRay"><tr>
<td class="line-numbers"><pre>1
2
3
4
</pre></td>
<td class="code"><pre><span class="tag"><script</span> <span class="attribute-name">type</span>=<span class="string"><span class="delimiter">"</span><span class="content">text/x-handlebars</span><span class="delimiter">"</span></span> <span class="attribute-name">id</span>=<span class="string"><span class="delimiter">"</span><span class="content">components/blog-post</span><span class="delimiter">"</span></span><span class="tag">></span>
<span class="inline"> <h1></span><span class="inline"><span class="inline-delimiter">{{</span><span class="attribute-name">title</span><span class="inline-delimiter">}}</span></span><span class="tag"></h1></span>
<span class="tag"><div</span> <span class="attribute-name">class</span>=<span class="string"><span class="delimiter">"</span><span class="content">body</span><span class="delimiter">"</span></span><span class="tag">></span><span class="inline"><span class="inline-delimiter">{{</span><span class="attribute-name">body</span><span class="inline-delimiter">}}</span></span><span class="tag"></div></span>
<span class="tag"></script></span>
</pre></td>
</tr></table>
</div></div>
<p>Now, we can use the <code>{{blog-post}}</code> component and pass it properties
in another template:</p>
<p>现在可以使用<code>{{blog-post}}</code>组件并且传递其他模板的属性到该组件:</p>
<div class="highlight handlebars "><div class="ribbon"></div><div class="scroller"><table class="CodeRay"><tr>
<td class="line-numbers"><pre>1
</pre></td>
<td class="code"><pre><span class="inline"><span class="inline-delimiter">{{</span><span class="attribute-name">blog-post</span> <span class="attribute-name">title</span>=<span class="attribute-value">title</span> <span class="attribute-name">body</span>=<span class="attribute-value">body</span><span class="inline-delimiter">}}</span></span>
</pre></td>
</tr></table>
</div></div>
<p><a class="jsbin-embed" href="http://jsbin.com/obogub/1/embed?live">JS Bin</a><script src="http://static.jsbin.com/js/embed.js"></script></p>
<p>(See <a href="/guides/components/passing-properties-to-a-component/">Passing Properties to a Component</a> for more.)</p>
<p>(关于如何传递属性给组件,请参看<a href="/guides/components/passing-properties-to-a-component/">Passing Properties to a Component</a>)</p>
<p>In this case, the content we wanted to display came from the model. But
what if we want the developer using our component to be able to provide custom
HTML content?</p>
<p>在本例中,用于显示的内容来源于模型。那么如果开发者想使用该组件来提供自定义的HTML内容该怎么做呢?</p>
<p>In addition to the simple form you've learned so far, components also
support being used in <strong>block form</strong>. In block form, components can be
passed a Handlebars template that is rendered inside the component's
template wherever the <code>{{yield}}</code> expression appears.</p>
<p>在已经描述的简单形式之外,组件还支持使用<strong>块</strong>的方式。在块方式下,可以传递一个Handlebars模板给组件,该模板将在组件模板中声明<code>{{yield}}</code>表达式的地方显示。</p>
<p>To use the block form, add a <code>#</code> character to the
beginning of the component name, then make sure to add a closing tag.
(See the Handlebars documentation on <a href="http://handlebarsjs.com/#block-expressions">block expressions</a> for more.)</p>
<p>为了使用块方式,需要在使用组件的时候,在组名前加上<code>#</code>,并确定添加了结束标签。(详细内容参见模板文档中的<a href="http://handlebarsjs.com/#block-expressions">块表达式</a>)。</p>
<p>In that case, we can use the <code>{{blog-post}}</code> component in <strong>block form</strong>
and tell Ember where the block content should be rendered using the
<code>{{yield}}</code> helper. To update the example above, we'll first change the component's
template:</p>
<p>在这种情况下,可以使用<code>{{blog-post}}</code>组件的<strong>块方式</strong>,并使用<code>{{yield}}</code>助手告知Ember块内容将被渲染到什么地方。为了更新上面的例子,首先需要修改组件的模板为:</p>
<div class="highlight handlebars "><div class="ribbon"></div><div class="scroller"><table class="CodeRay"><tr>
<td class="line-numbers"><pre>1
2
3
4
</pre></td>
<td class="code"><pre><span class="tag"><script</span> <span class="attribute-name">type</span>=<span class="string"><span class="delimiter">"</span><span class="content">text/x-handlebars</span><span class="delimiter">"</span></span> <span class="attribute-name">id</span>=<span class="string"><span class="delimiter">"</span><span class="content">components/blog-post</span><span class="delimiter">"</span></span><span class="tag">></span>
<span class="inline"> <h1></span><span class="inline"><span class="inline-delimiter">{{</span><span class="attribute-name">title</span><span class="inline-delimiter">}}</span></span><span class="tag"></h1></span>
<span class="tag"><div</span> <span class="attribute-name">class</span>=<span class="string"><span class="delimiter">"</span><span class="content">body</span><span class="delimiter">"</span></span><span class="tag">></span><span class="inline"><span class="inline-delimiter">{{</span><span class="attribute-name">yield</span><span class="inline-delimiter">}}</span></span><span class="tag"></div></span>
<span class="tag"></script></span>
</pre></td>
</tr></table>
</div></div>
<p>You can see that we've replaced <code>{{body}}</code> with <code>{{yield}}</code>. This tells
Ember that this content will be provided when the component is used.</p>
<p>由此可见,<code>{{body}}</code>被替换为<code>{{yield}}</code>。这告知Ember这里的内容会在组件使用的时候提供。</p>
<p>Next, we'll update the template using the component to use the block
form:</p>
<p>接下来,更新使用组件的模板采用块方式:</p>
<div class="highlight handlebars "><div class="ribbon"></div><div class="scroller"><table class="CodeRay"><tr>
<td class="line-numbers"><pre>1
2
3
4
</pre></td>
<td class="code"><pre><span class="inline"><span class="inline-delimiter">{{#</span><span class="attribute-name">blog-post</span> <span class="attribute-name">title</span>=<span class="attribute-value">title</span><span class="inline-delimiter">}}</span></span>
<span class="tag"><p</span> <span class="attribute-name">class</span>=<span class="string"><span class="delimiter">"</span><span class="content">author</span><span class="delimiter">"</span></span><span class="tag">></span>by <span class="inline"><span class="inline-delimiter">{{</span><span class="attribute-name">author</span><span class="inline-delimiter">}}</span></span><span class="tag"></p></span>
<span class="inline"><span class="inline-delimiter">{{</span><span class="attribute-name">body</span><span class="inline-delimiter">}}</span></span>
<span class="inline"><span class="inline-delimiter">{{/</span><span class="attribute-name">blog-post</span><span class="inline-delimiter">}}</span></span>
</pre></td>
</tr></table>
</div></div>
<p><a class="jsbin-embed" href="http://jsbin.com/osulic/1/embed?live">JS Bin</a><script src="http://static.jsbin.com/js/embed.js"></script></p>
<p>It's important to note that the template scope inside the component
block is the same as outside. If a property is available in the template
outside the component, it is also available inside the component block.</p>
<p>这里需要注意的时,在组件块中的作用域与模板的作用域与外部模板的作用域是相同的。如果一个属性在组件外部的模板有效,那么其在组件块内部也有效。</p>
<p>This JSBin illustrates the concept:</p>
<p>下面的JSBin展示了这个概念:</p>
<p><a class="jsbin-embed" href="http://jsbin.com/iqocuf/1/embed?live">JS Bin</a><script src="http://static.jsbin.com/js/embed.js"></script></p>
<footer>
<a class="previous-guide" href="/bilingual_guides/components/passing-properties-to-a-component">
← 传递属性
</a>
<a class="next-guide" href="/bilingual_guides/components/customizing-a-components-element">
自定义组件元素 →
</a>
</footer>
</div>
</div>
</div>
<div id="footer">
<div id="footer-wrapper">
<div class="info">© 2013 Emberjs.CN <br/>Design by <a href="http://www.heropixel.com">HeroPixel</a></div>
<div class="statement"><b>声明:</b>本站的文章可以随意在网上转载,但必须注明原文出处!</div>
<div class="links">
<a href="http://weibo.com/embercn"><img src="/images/weibo.png"/></a>
<a href="http://emberjs.cn/blog/feed.xml"><img src="/images/feed.png"/></a>
</div>
</div>
</div>
<script type="text/javascript">
var _bdhmProtocol = (("https:" == document.location.protocol) ? " https://" : " http://");
document.write(unescape("%3Cscript src='" + _bdhmProtocol + "hm.baidu.com/h.js%3F0b0fcec01b06cc76e274f7b21f4766c4' type='text/javascript'%3E%3C/script%3E"));
$(function() {
$('a[href="http://tongji.baidu.com/hm-web/welcome/ico?s=0b0fcec01b06cc76e274f7b21f4766c4"]').hide();
});
</script>
</body>
</html>