gpt4 book ai didi

javascript - 从每个助手调用的组件中产生

转载 作者:可可西里 更新时间:2023-11-01 02:32:15 25 4
gpt4 key购买 nike

这是我的组件模板的一部分:

{{#each displayResults}}
<li {{action addSelection this}} {{bindAttr class=\":result active\"}}>
{{#if controller.template}}
{{yield}}
{{else}}
<span class=\"result-name\">{{displayHelper controller.searchPath}}</span>
{{/if}}
<\/li>
{{/each}}

我希望用户能够自定义用于显示结果的 html。

问题是 {{yield}} 在 {{#each}} 助手中被调用,如果组件声明如下:

{{#auto-suggest source=controller.employees destination=controller.chosenEmployees}}
<span class=\"result-name\"><img src="img/small_avatar.png"/>{{fullName}}</span>
{{/auto-suggest}}

然后 {{#auto-suggest}} 之间的 block 没有组件中 {{#each}} 助手的上下文。

有什么我可以做的吗?还是就是这样?

最佳答案

更新

现在ember 1.10着陆后,引入了一种称为 block 参数的新语法。所以不需要重写_yield方法。例如,在您的组件模板中,您可以:

<ul>    
{{#each item in source}}
<li>
{{! the component is being used in the block form, so we yield}}
{{#if template.blockParams}}
{{yield item}}
{{! no block so just display the item}}
{{else}}
{{item}}
{{/if}}
</li>
{{/each}}
</ul>

然后在使用组件时使用 as |var|

将参数传递给 {{yield}}
{{! no block, the component will just display the item}}
{{auto-suggest source=model as |item|}}

{{! in the block form our custom html will be used for each item}}
{{#auto-suggest source=model as |item|}}
<h1>{{item}}</h1>
{{/auto-suggest}}

Simple live example

当然,您可以使用 {{yield name age occupation hobbies}} 产生任何变量,并在组件中捕获它们:

{{#x-foo user=model as |name age occupation hobbies|}}
Hi my name is {{name}}, I am {{age}} years old. Major of the times I am {{occupation}}, but also love to {{hobbies}}.
{{/x-foo}}

旧版本

您可以覆盖 Ember.Component 的默认 _yield 方法,并更改 context:get(parentView, 'context')context: get(view, 'context')

App.AutoSuggestComponent = Ember.Component.extend({
_yield: function(context, options) {
var get = Ember.get,
view = options.data.view,
parentView = this._parentView,
template = get(this, 'template');

if (template) {
Ember.assert("A Component must have a parent view in order to yield.", parentView);
view.appendChild(Ember.View, {
isVirtual: true,
tagName: '',
_contextView: parentView,
template: template,
context: get(view, 'context'), // the default is get(parentView, 'context'),
controller: get(parentView, 'controller'),
templateData: { keywords: parentView.cloneKeywords() }
});
}
}
});

关于javascript - 从每个助手调用的组件中产生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20108321/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com