简体   繁体   English

如何将索引从模板循环传递到我的助手中,并使其返回Handlebars表达式

[英]How can I pass the index from a template loop into my helper, and have it return a Handlebars expression

The problem stems from the inability to do comparisons in the template: 问题源于无法在模板中进行比较:

{{#if model.input.type == 'select'}}

The option around this, that I'm aware of so far, is to use a custom Handlebars helper. 到目前为止,我所知道的解决方法是使用自定义的Handlebars帮助器。 This would allow me to loop through the model and build out the HTML, but this doesn't allow me to bind attributes, plus just it seems like too much work. 这将允许我遍历模型并构建HTML,但这不允许我绑定属性,而且似乎工作太多。

1. Can't bind attributes, and I don't want to build out HTML with a JS string.   

Handlebars.registerHelper('formBuilder', function()
{
    var html_string = '';

    for(var q=0; q<this.content.questions.length; q++)
    {
        if(this.content.questions[q].type.match(/select/i))
             { /* build out html with model propeties */ }

        if(this.content.questions[q].type.match(/checkbox|radio/i))
             { /* build out html with model propeties */ }

    }
    return new HandleBars.SafeString(html_string);
}

A better option would be if I could track the index in my helper, and return a Handlebars expression, all without breaking the template's loop: 一个更好的选择是,如果我可以在助手中跟踪索引并返回Handlebars表达式,而又不会破坏模板的循环:

        2. Can't pass in loop index or return expressions.

        {{#each question in model.questions}}
            {{#formBuilder parent.index this}}
                /* uses @index to iterate through its children and return a Handlebars 
                   expressions for either select or checkbox html */
            {{/formBuilder}}
        {{/each}}

...and the formBuilder helper would look something like this: ...以及formBuilder帮助器将如下所示:

Handlebars.registerHelper('formBuilder', function(q_index, model)
{
    if(model[q_index].type == 'select')
    {
        return Handlebars.compile({{select action 'myAction' type='model[q_index].type'}});
    }

    if(model[q_index].type.match(/checkbox|radio/i))
    {
        return Handlebars.compile({{input action 'myAction' type='model[q_index].type'}});
    }

});

wich would return controll to the outer loop. 这将使控制返回到外部循环。

How is this problem solved in Ember? 如何在Ember中解决此问题?

Looking at what you are trying to achieve more closely, I would suggest you use a view instead of a handlebars helper to create either a checkbox or a select field. 仔细观察您要实现的目标,建议您使用视图而不是车把帮助器来创建复选框或选择字段。 The way you would do that would be 你这样做的方式是

{{#each question in questions}}
  {{view QuestionView contentBinding=this}}
{{/each}}

and in your questionView, 在您的questionView中,

 App.QuestionView = Em.View.extend({

   defaultTemplate: (function() {
      if(this.get('type') === 'select') {
        return Em.Handlebars.compile(//select box string goes here);
      }
      else {
        return Em.Handlebars.compile(//checkbox html);
      }
   })()
 });

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

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