不从collection.toJSON()渲染的骨干视图

时间:2013-05-08 14:05:01

标签: backbone.js handlebars.js

我正在使用骨干,把手,jquery for app。以下代码行无效

this.$el.find('.quiz-container').append(quiz.tmplt.result(this.collection.toJSON()));

模板

<h2 class="score">Congratulation ABCD! You have scored {{score}} !!!</h2>
<table>
    <thead>
        <tr>
            <th>Question</th>
            <th>Your Answer</th>
            <th>Correct Answer</th>
        </tr>
    </thead>
    <tbody>
        {{#each qtext}}
        <tr>
            <td>{{this}}</td>
            <td>
                {{#each ../answer}}
                <span>{{this}}</span>
                {{/each}}
            </td>           
        </tr>
        {{/each}}
    </tbody>
</table>

这是我的JSON文件

[{
    "qid": "1001",
    "qtext": "This is question one?",
    "options": [{"id":"opt1", "str":"15%"},{"id":"opt2", "str": "16%"},{"id":"opt3", "str": "17%"},{"id":"opt4", "str": "19%"}],
    "answer": ["opt2"]},
    {
    "qid": "1002",
    "qtext": "This is question two?",
    "options": [{"id":"opt1", "str":"item0"},{"id":"opt2", "str": "item1"},{"id":"opt3", "str": "item2"},{"id":"opt4", "str": "item3"}],
    "answer": ["opt1"]},
    {
    "qid": "1003",
    "qtext": "This is question three?",
    "options": [{"id":"opt1", "str":"5%"},{"id":"opt2", "str": "2.5%"},{"id":"opt3", "str": "11%"},{"id":"opt4", "str": "0%"}],
    "answer": ["opt3"]}
]

我知道,我可以拆分这个模板并使用collection.each方法我可以从JSON文件渲染每个对象,但我想保留它第一个模板并直接从集合数据渲染。

提前致谢

维克拉姆

0 个答案:

没有答案