问题描述
我看到,ember有一个非常好的机制,使用 {{yield}}
机制记录在一个组件中包装内容。
I see that ember has a very nice mechanism for wrapping content in a component using the {{yield}}
mechanism documented here.
在文档中的示例中,我可以使用如下定义的 blog-post
组件模板:
So, to use the example in the documentation, I can have a blog-post
component template defined like so:
<script type="text/x-handlebars" id="components/blog-post">
<h1>{{title}}</h1>
<div class="body">{{yield}}</div>
</script>
然后我可以将 blog-post
任何其他使用以下格式的模板:
I can then embed blog-post
into any other template using the form:
{{#blog-post title=title}}
<p class="author">by {{author}}</p>
{{body}}
{{/blog-post}}
我的问题是,我可以在组件模板中指定两个不同的 {{yield}}
My question is, can I specify two different {{yield}}
outlets in the components template?
可以通过在 Ember.Route#renderTemplate
像这样:
Handlebars :
<div class="toolbar">{{outlet toolbar}}</div>
<div class="sidebar">{{outlet sidebar}}</div>
JavaScript :
App.PostsRoute = Ember.Route.extend({
renderTemplate: function() {
this.render({ outlet: 'sidebar' });
}
});
我不知道我可以采取这个路径的组件,不知道什么路由的模板
I'm not sure I can take this path for a component which will not know what route's template would be rendering it.
编辑1:
为了清楚起见,我正在尝试实施Android 作为Ember组件。
For the sake of clarity, I'm trying to implement the Android Swipe for Action Pattern as an Ember component.
所以,我希望这个组件的用户能够指定两个不同的模板:
So, I'd like users of this component to be able to specify two different templates:
- 正常列表项目的模板,
- 检测到扫描(1)时显示的操作模板。
我想把它变成一个组件,因为相当多的javascript来处理触摸(开始/移动/结束)事件,同时仍然管理列表中的。用户将提供两个模板,该组件将管理触摸事件和必要动画的处理。
I want to make this into a component, because quite a lot of javascript goes into handling the touch(start/move/end) events, while still managing smooth touch based scrolling of the list. Users would supply the two templates and this component would manage handling of touch events and necessary animations.
我已经设法使组件以块形式工作,其中块的内容被视为(1)。第二个模板(2)通过参数( actionPartial
)指定,该参数是模板的操作:
I've managed to get the component working in the block form, where the block's contents are treated like (1). The second template (2) is specified through a parameter (actionPartial
below) which is the name of a partial template for the actions:
组件手柄模板:sfa-item.handlebars
<div {{bind-attr class=":sfa-item-actions shouldRevealActions:show" }}>
{{partial actionPartial}}
</div>
<div {{bind-attr class=":sfa-item-details isDragging:dragging shouldRevealActions:moveout"}}>
{{yield}}
</div>
拨打Handlebars模板:
{{#each response in controller}}
<div class="list-group-item sf-mr-item">
{{#sfa-item actionPartial="mr-item-action"}}
<h5>{{response.name}}</h5>
{{/sfa-item}}
</div>
{{/each}}
mr-item -action
句柄定义如下:
mr-item-action.handlebars :
<div class="sf-mr-item-action">
<button class="btn btn-lg btn-primary" {{action 'sfaClickedAction'}}>Edit</button>
<button class="btn btn-lg btn-primary">Delete</button>
</div>
问题是,用户提供的部分操作, sfaClickedAction
以上,没有从组件冒泡。第4段中中提到的事实
Problem is, actions from the user supplied partial, sfaClickedAction
above, are not bubbled up from the component. A fact which is mentioned in the docs in para 4.
所以,现在我不知道用户如何捕获他在提供的动作模板中定义的动作。组件不能捕捉到这些操作,因为它们也不知道它们。
So, now I do not know how a user could capture actions that he defined in the supplied actions template. A component cannot catch those actions because it doesn't know about them either.
编辑2
我发布了后续问题
推荐答案
由于不可能有两个 {{yield}}
一个组件中的助手(组件如何知道哪里一个 {{yield}}
的标记停止,下一个开始?)你可以从不同的方向解决这个问题。
Since it is not possible to have two {{yield}}
helpers within one component (how would the component know where one {{yield}}
's markup stops and the next one begins?) you may be able to approach this problem from a different direction.
考虑嵌套组件的模式。浏览器已经取得了巨大成功。例如,采取< ul>
和< li>
组件。 a < ul>
想要采取许多位的标记,并且像列表的成员一样呈现每个标记。为了完成此操作,它强制您将分项标记分成< li>
标签。还有很多其他例子。 < table>
,< tbody>
,< tr>
,< td>
是另一个很好的例子。
Consider the pattern of nested components. Browsers do this already with great success. Take, for example, the <ul>
and <li>
components. A <ul>
wants to take many bits of markup and render each one like a member of a list. In order to accomplish this, it forces you to separate your itemized markup into <li>
tags. There are many other examples of this. <table>
, <tbody>
, <tr>
, <td>
is another good case.
我想你可能偶然发现在那里你可以实现这种模式。例如:
I think you may have stumbled upon a case where you can implement this pattern. For example:
{{#sfa-item}}
{{#first-thing}}
... some markup
{{/first-thing}}
{{#second-thing}}
... some other markup
{{/second-thing}}
{{/sfa-item}}
显然第一件
和第二件
是您的专门组件的可怕名称,代表您想要包装的东西第一个和第二个模板。你得到这个想法。
Obviously first-thing
and second-thing
are terrible names for your specialized components that represent the things you'd want to wrap with your first and second templates. You get the idea.
请注意,嵌套组件将无法访问外部组件中的属性。如果两者都需要,则必须将值与外部和内部组件绑定。
Do be careful since the nested components won't have access to properties within the outer component. You'll have to bind values with both outer and inner components if they are needed in both.
这篇关于Ember.Component(块形式):多个出口{{yield}}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!