ember.js中Component模板使用中的yield as

如果你不喜欢广告,请收藏我的网站,下次在网站中搜索
用真金白银赞赏有价值的内容,请通过文末二维码打赏

在ember.js文档的Wrapping Content in a ComponentUsing Block Params两节中,在文档里面出现了这样一段模板代码:

{{#blog-post editStyle="markdown" as |post|}}
  <p class="author">by {{author}}</p>
  {{post.body}}
{{/blog-post}}

当我看到as |post|的时候就懵圈儿了。在前面的文档中有{{#each model as |item|}}{{/each}}的用法,但是此处的as |post|显然是和each遍历不同的。花了一些时间,把这个问题搞清楚了。

yield的意思就是“传给”的意思,只要在使用{{#}}{{/}}这种块级形式的组件标签时,才使用。我们通过三个演示来说明。

三种不同的调用演示

我们创建test的router、controller、template,以及创建一个post-info组件。然后,我们在app/controllers/test.js中写入如下代码:

import Ember from 'ember';

export default Ember.Controller.extend({
  title : 'title name',
  body : 'this is my test body',
  author : 'Author'
  postData : {
    title : 'the title in sub',
    body : 'body not the large',
    author : 'contributor'
  }
});

这里我们准备好了预设数据,将会在模板、组件模板中进行调用作为演示。

1 直接调用controller中的数据

我们先使用yield直接输出,在app/templates/components/post-info.hbs中加入如下代码:

{{yield}}

就这样一个短短的代码就可以了。接下来在app/templates/test.hbs中如下写入:

<h1>TEST</h1>

{{#post-info}}
<article>
 <h2>{{title}}</h2>
 <p>{{body}}</p></article>
{{/post-info}}

然后我们运行ember,可以看到,{{post-info}}区块的内容被展示出来。而且区块内容中的{{title}}{{body}}均使用controller中的数据。我们把post-info.hbs改为如下:

<div>before yield</div>
{{yield}}
<div>after yield</div>

运行之后,我们发现区块内容被放在{{yield}}的位置。最终构造出来的HTML是:

<h1>TEST</h1>

<div>before yield</div>
<article>
  <h2>title name</h2>
  <p>this is my test body</p>
</article>
<div>after yield</div>

由此我们得出结论:

  1. 主模板中的区块内容将被放在组件模板的{{yield}}位置(这一规则在下面的所有情况中也有效)。
  2. 在未做任何yield预设的情况下,区块中的变量由主模板决定。

2 在yield中使用传入的数据

我们把post-info.hbs修改为:

{{yield post.title post.body}}

把test.hbs修改为:

<h1>TEST</h1>

{{#post-info post=postData as |title body|}}
<article>
 <h2>{{title}}</h2>
 <p>{{body}}</p>
</article>
{{/post-info}}

这个时候再看返回的结果,就会发现原本title和body发生了变化,变为我们在controller中给的postData的值。

这样看的效果并不明显,我们把test.hbs改为:

<h1>TEST</h1>

{{#post-info post=postData as |test1 test2|}}
<article>
 <h2>{{test1}}</h2>
 <p>{{test2}}</p>
</article>
{{/post-info}}

这个时候你就应该发现规律了,test1是postData.title的值,test2是postData.body的值。我们得出结论:

{{yield param1 param2 ...}}中的param被作为as |output1 output2 ...|中的output。(注意,我们这里将postData通过post传入到了组件模板中,所以组件模板中的post实际上是被传入的postData的数据。)

看了一下文档,这种声明还可以按照下面这种形式进行:

{{#if hasBlock}}
  {{yield post.title}}
  {{yield post.body}}
  {{yield post.author}}
{{else}}
  <h2>{{post.title}}</h2>
  <p class="author">Authored by {{post.author}}</p>
  <p>{{post.body}}</p>
{{/if}}

使用多个{{yield}}其实等同于使用一个{{yield}},它的变量顺序是一样的。但是这种组件模板可以更好的根据主模板中是否是以块级的形式调用。

3 使用hash声明

我们接下来继续修改post-info.hbs为:

{{yield (hash title='just title for fun' body='this body is for fun')}}

把test.hbs修改为:

<h1>TEST</h1>

{{#post-info as |post|}}
<article>
  <p>{{post.title}}</p>
  <p>{{post.body}}</p>
</article>
{{/post-info}}

再来看下运行结果。发现主模板中的|post|被代入到主模板中的区块内容中,成为一个变量,而它还有title和body两个属性,都能输出我们想要的值。

我们在来混合使用一下,把post-info.hbs改为:

{{yield (hash title=post.title body=post.body) post.author}}

把test.hbs改为:

<h1>TEST</h1>

{{#post-info post=postData as |post author|}}
<article>
  <p>{{title}}</p>
  <p><small>{{author}}</small></p>
  <p>{{body}}</p>
</article>

<article>
  <p>{{postData.title}}</p>
  <p><small>{{postData.author}}</small></p>
  <p>{{postData.body}}</p>
</article>
<article>
  <p>{{post.title}}</p>
  <p><small>{{author}}</small></p>
  <p>{{post.body}}</p>
</article>
{{/post-info}}

运行查看结果,上面的三个article标签中,第一个article标签模拟使用controller中的值,第二个article标签中模拟使用controller中的postData的属性来进行调用,第三个article则模拟使用as |post author|中的变量。有一点需要注意,就是第一个article中的{{author}}实际上已经被as后的author覆盖了,所以显示的时候不会是Author,而是contributor。

而实际上,使用hash声明的时候,yield的变量仍然遵循上面第2点中的规则,就是yield后面传入多少个变量,在as后面就可以声明多少个变量,括号的作用是把这个括号当作一个变量来对待,hash的作用是将括号所代表的变量添加属性

由此,我们得出这样的结论:

yield中可以使用hash对某个预定义变量进行属性规则规定。

 

遗留问题,是否能如下使用yield(带=赋值):

{{yield post=post author='IamAuthor'}}

本文中的这个知识点涉及到一个“闭包”的概念,当然这里不是意义上的闭包,但是了解闭包概念,在此处的理解上会容易一些。

在主模板和组件模板之间,存在相互传值的过程,这也就改变了原来ember的模板变量值的来源,在原始的ember模板变量赋值过程中,需要借助controllers或组件来完成,也可以像行内调用的方式,实现传值。但是当采用我们本文讨论的方法之后,组件的模板中可以预先设定好一些值,反传给主模板,这就突破了原始的ember模板变量值的来源。

2016-05-26 4912

为价值买单,打赏一杯咖啡

本文价值49.12RMB