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

问题描述

这里是CreditCards控制器上下文
{{#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>
{{/是}}
{{/ each}}
{{/ with}}

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



As symbol (例如,age_to_apply)涉及到所有我可以想出的信用卡字段( ageToApply )是使用 cardProperty 帮助器,将当前卡片上下文( this )并解析 property.symbol ,例如:

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

理想情况下,我无需帮助者就可以这样做:

  *** {{property.symbol}}应该查找这个上下文
#并成为例如{{annual_fee}}或{{annualFee}} - 是可能吗?
{{#with controllers.currentCardCategory}}
{{#each property in cardProperties}}
< td class =td> {{property.symbol}} *** </td>
{{/ each}}
{{/ with}}

但是问题是我不知道如何渲染{{yourSavings}}部分。您可以看到的帮助者来自。不幸的是,帮助者不能解析属性,以便 property.symbol 成为一个字符串。



这里是: / p>

  Handlebars.registerHelper'是',(value,test,options) - > 
如果值为test then options.fn(@)else options.inverse(@)

我认为这是可能的,但 - 不知道哪一个,虽然。



我想避免的是诉诸计算属性,如如果isYourSavings

解决方案

我不确定你的代码的上下文,但是似乎你是寻找 registerBoundHelper 与块帮助器。这不支持您将遇到此警告,

另一种方法来做你正在做的是使用视图助手。视图助手就像一个帮手,但可以使用自定义模板呈现。



例如,一个 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>

你可以这样使用帮手,



$ $ $ $ $ {$ {$ {code $ {$#code $ b {{/ each}}
{{/ with}}

任何数量的属性作为属性。这些将绑定到 CardItemView 。而且由于它是视图中的任何视图,如自定义的计算属性,可以在 CardItemView 中完成。


# 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}}

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.

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}}

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.

Here it is:

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.

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

解决方案

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,

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.

For instance a CardItemView would be,

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

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

Where the cardItemTemplate is,

<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}}

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.

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

08-12 10:14