我正在使用 Marionette 尝试渲染问题列表。每个问题都可以有一个问题类型的选择列表,这些问题类型也将来自服务器。我更愿意将此列表保留在 json 的顶层而不是每个问题上。有什么方法可以遍历 ItemView 中的问题,但仍然可以访问模板中的问题类型?
我可以将其作为一个每次重新渲染的大型模板来执行,但我希望避免这种情况。
JSON:
var json = {
questions: [
{ id: 1, questionType: "FreeText", label: "What is your name?" }
],
questionTypes: [
{ label: "Free Text", qtype: "FreeText" },
{ label: "Single Select", qtype: "Select" },
{ label: "Multi Select", qtype: "MultiSelect" }
]
};
模板:
{{#each questions}}
<div class="question-editor">
<button class="remove-question" data-id="{{this.id}}">X</button>
<label for="QuestionType">Question Type:</label>
<select name="QuestionType">
{{#each ../questionTypes}}
<option value="{{this.qtype}}" {{#isOptionSelected ../questionType this.qtype}} selected="selected" {{/isOptionSelected}}>{{this.label}}</option>
{{/each}}
</select>
<br />
<label for="Label">Label:</label>
<input type="text" name="Label" value="{{this.label}}" />
</div>
{{/each}}