本文介绍了自定义把手助手 - 参数未解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

# here is CreditCards controller context
{{#with controllers.currentCardCategory}}
  {{#each property in cardProperties}}
    {{#is property.symbol 'your_savings'}}
      <td class="td">{{yourSavings}}</td>
    {{else}}
      <td class="td">{{cardProperty this property.symbol}}</td>
    {{/is}}
  {{/each}}
{{/with}}

我动态创建表.所有内容都来自 ArrayController,除了一个来自控制器的计算属性.symbol' 字段像annual_fee' 一样带有下划线,属于CreditCardProperty.每个卡片类别都有不同的卡片属性集.只有两个类别具有属性(类别具有许多卡片属性),并且一个记录将 computed 字段设置为 true.这意味着模板应该查找相应的控制器.

I create the table dynamically. All content comes from ArrayController except one that is a computed property that comes from the controller. symbol' field is underscored likeannual_fee' and belongs to CreditCardProperty. Each card category has different set of card properties. Only two categories have properties (category has many card properties) and one record has computed field set to true. That means that the template should look up the corresponding controller.

由于symbol(例如age_to_apply)与CreditCard 字段之一(ageToApply)相关,我所能想到的就是使用cardProperty 在上下文中获取当前卡片 (this) 并解析 property.symbol 的助手,例如:

As symbol (e.g age_to_apply) relates to one of the CreditCard fields (ageToApply) all I could to figure out was to use the cardProperty helper which takes current card in the context (this) and resolves property.symbol, e.g:

camelizedProperty = categoryProperty.camelize()
card.get 'camelizedProperty'

理想情况下,我希望没有助手并像这样使用它:

Ideally I'd like to do without the helper and use it somehow like this:

# *** {{property.symbol}} should look up this context
#     and become e.g {{annual_fee}} or {{annualFee}} - is it possible?
{{#with controllers.currentCardCategory}}
  {{#each property in cardProperties}}
    <td class="td">{{property.symbol}}***</td>
  {{/each}}
{{/with}}

但问题是我不知道如何渲染{{yourSavings}}"部分.您可以看到的帮助程序来自 Handlebars 帮助程序的 swag 集合.不幸的是,帮助程序没有解析属性,因此 property.symbol 变成了一个字符串.

But the problem is that I don't know how can I render that '{{yourSavings}}' part. The helper you can see comes from swag collection of Handlebars helpers. The helper, unfortunately does not resolve properties so that property.symbol becomes a string.

这是:

Handlebars.registerHelper 'is', (value, test, options) ->
  if value is test then options.fn(@) else options.inverse(@)

我认为这是可能的,但是正确帮手 - 但不知道是哪一个.

I think it is possible but with the right helper - don't know which one, though.

我想要避免的是求助于像 if isYourSavings 这样的计算属性.

What I do would like to avoid is to resort to computed property like if isYourSavings.

推荐答案

我不确定您的代码的上下文,但您似乎正在寻找带有块助手的 registerBoundHelper.这不受支持.你会遇到这个警告,

I am not certain about the context of your code, but it seems like you are looking for registerBoundHelper with a block helper. This isn't supported. You will run into this warning,

断言失败:registerBoundHelper 生成的助手不支持与 Handlebars 块一起使用.

另一种做你正在做的事情的方法是使用视图助手.视图助手类似于助手,但可以使用自定义模板进行渲染.

An alternative way to do what you are doing is to use a view helper instead. A view helper is like a helper but can render with a custom template.

例如,CardItemView 将是,

App.CardItemView = Em.View.extend({
  templateName: 'cardItemTemplate'
});

Em.Handlebars.registerHelper('cardItem', App.CardItemView);

cardItemTemplate 在哪里,

<script type='text/x-handlebars' data-template-name='cardItemTemplate'>
  {{#if view.property.symbol}}
    <td class="td">{{yourSavings}}</td>
  {{else}}
    <td class="td">{{cardProperty view.property.symbol}}</td>
  {{/if}}
</script>

你可以像这样使用助手,

And you could use the helper like so,

{{#with controllers.currentCardCategory}}
  {{#each property in cardProperties}}
    {{cardItem property=property etc}}
  {{/each}}
{{/with}}

您可以传入任意数量的属性作为属性.这些将绑定到 CardItemView.因为它是一个视图,所以视图所做的任何事情,比如自定义计算属性,都可以在 CardItemView 中完成.

You can pass in any number of properties as attributes. These will be bound to the CardItemView. And since it's a view anything a view does, like custom computed properties, can be done in CardItemView.

这篇关于自定义把手助手 - 参数未解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 10:14