简体   繁体   English

如何将#if类型车把助手从ember 1.7转换为ember-cli(2.3)

[英]How do I translate #if type handlebars helpers from ember 1.7 to ember-cli (2.3)

I have this registered handlebars helper in ember1.7 right now. 我现在在ember1.7中有此已注册的把手助手。

var get = Ember.Handlebars.get;
Ember.Handlebars.registerHelper('ifCond', function(val1, val2, options) {
    var context = (options.fn.contexts && options.fn.contexts[0]) || this;
    var val1 = get(context, val1, options.fn);
    if (val1 == val2) {
        return options.fn(this);
    } else {
        return options.inverse(this);
    }
});

The idea is pretty simple: take first parameter as a context property, second parameter as an actual value, and return a boolean based on their equality. 这个想法很简单:将第一个参数作为上下文属性,将第二个参数作为实际值,然后根据它们的相等性返回布尔值。 So, for example, if I have a property age on an object user, 因此,例如,如果我对某个对象用户拥有财产年龄,

{{#ifCond user.age "22" }}
  <h2> Twin Twos!</h2>
{{/ifCond}}

would be a snippet I would use to render the h2 element when the condition is fulfilled. 将是满足条件时用于呈现h2元素的摘要。

However, I cannot figure out how to translate such a helper for ember-cli. 但是,我不知道如何为ember-cli翻译这样的助手。

I've considered making a component, but I need a conditional, not something that renders a specific set of DOM elements. 我已经考虑过制造一个组件,但是我需要一个条件组件,而不是呈现特定DOM元素集合的组件。 I feel like I'm missing something. 我觉得我想念什么。 How would I be able to make {{#ifCond}} work in Ember 2.3? 如何在Ember 2.3中使{{#ifCond}}工作?

EDIT: I found a helper creation doc that let me to this implementation: 编辑:我找到了一个帮助程序创建文档,该文档可用于该实现:

import Ember from 'ember';

export function ifCond(params/*, hash*/) {
    return (params[0] === params[1]);
  // return params;
}

export default Ember.Helper.helper(ifCond);

however, for this to work, I would have to write (in the template) 但是,要使其正常工作,我将不得不写(在模板中)

{{#if (ifCond 1 1)}}
  yep
{{/if}}

Is there still a way to actually use the handlebar helper as we could in ember 1.7 ? 像在ember 1.7中一样,还有没有一种方法可以实际使用车把帮助器?

After digging around for a good while, it seems that the question edit is the best way forward. 经过一段时间的研究,问题编辑似乎是最好的方法。 It is the accepted way of handlebar helpers from here on out, it seems. 从现在开始,这是车把帮手的公认方法。

To create a helper, emulate: 要创建一个助手,请模拟:

import Ember from 'ember';

export function ifCond(params/*, hash*/) {
    return (params[0] === params[1]);
  // return params;
}

export default Ember.Helper.helper(ifCond);

Then, include it in the template in this format. 然后,以这种格式将其包含在模板中。

{{#if (ifCond 1 1)}}
  yep
{{/if}}

This link pointed me towards the right direction: https://guides.emberjs.com/v2.3.0/templates/writing-helpers/ 该链接为我指明了正确的方向: https : //guides.emberjs.com/v2.3.0/templates/writing-helpers/

And this one showed me exactly what could be done: http://emberigniter.com/how-to-equals-conditional-comparison-handlebars/ 这正是向我展示了可以做什么: http : //emberigniter.com/how-to-equals-conditional-comparison-handlebars/

First of all there are no more block helpers : 首先, 不再有块帮助器

Legacy Handlebars helpers are removed in favor of the Ember.Helper API. 传统的车把助手被删除,以使用Ember.Helper API。 This API does not provide a mechanism for helpers to take a block, but does introduce support for nested helpers which can be used in concert with built-in helpers (like {{#if}} and {{#each}}) to achieve the same ends. 该API并未提供帮助者采取阻止措施的机制,但确实引入了对嵌套帮助者的支持,该支持可与内置帮助者(例如{{#if}}和{{#each}})一起使用以实现同样的目的。

It is recommended to use the helpers just as you did in your example ( {{#if (ifCond 1 1)}} ). 建议像在示例中一样使用辅助程序( {{#if (ifCond 1 1)}} )。 If you would decide to go this route I would recommend using the ember-truth-helpers addon. 如果您决定走这条路,我建议您使用ember-truth-helpers插件。 Using it you can throw out your own helper and use it like this: 使用它,您可以抛弃自己的助手,并像这样使用它:

{{#if (eq user.age "22") }}
  <h2> Twin Twos!</h2>
{{/if}}

I find the truth helpers very flexible and easy to understand and would recommend using them. 我发现真相帮助程序非常灵活且易于理解,建议您使用它们。

Alternatively if you want to simulate your old helper you need to use a component for that, I've made a twiddle to illustrate how you could do it. 另外,如果您想模拟旧的助手,则需要为此使用一个组件,我已经进行了一次旋转来说明如何实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM