从Handlebars模板中的数组中选择特定项

时间:2013-10-01 13:24:55

标签: javascript templates handlebars.js

如果我将以下2个数组绑定到已编译的Handlebars模板:

questions: [
    {id:1, text:'question1', answers: [
        {id: 1, text:'answer1'},
        {id: 2, text:'answer2'}
    ]},
    {id:2, text:'question2', answers: [
        {id: 1, text:'answer3'},
        {id: 2, text:'answer4'}
    ]}
],
userAnswers: [
    {questionId:1, answerId:2}
]

我宁愿不改变数据结构,需要执行以下操作(伪代码):

for each (question in questions) 
    render question text
    render user answer text

(请注意,可能会有一个没有答案的问题)

如果我在Handlebars模板中使用'each':

{{#each question}}

有没有办法从userAnswers对象获取答案ID?

然后将此映射回问题以获取文本?

e.g。是否有某种选择器语法,如:

{{../userAnswers[questionId=id].answerId}}

0 个答案:

没有答案