流星火焰与环路动态变量绑定

时间:2015-07-26 18:56:57

标签: mongodb meteor meteor-blaze

我有两个商店:学生和作业。每份作业文件都包含学生成绩:

{
  _id: 'assignment_id_here',
  name: 'Homework Assignment 1',
  grades: {
    'id_of_student_1': 88,
    'id_of_student_2': 93,
    'id_of_student_7': 76
  }
}

我的模板可以访问学生文档和作业。我想显示每个学生的成绩(上面是88,93和76)。

<div>Assignment Name: {{assignment.name}}</div>
<div>Grades</div>
{{#each students}}
  Student {{name}} with student ID {{_id}} received {{assignment.grades[_id]}} on this assignment
{{/each}}

assignment.grades[_id]不起作用!什么会奏效?有没有更好的方法来构建这里看到的文件?

2 个答案:

答案 0 :(得分:1)

构建文档的更好方法是采用此模式,将学生成绩作为嵌入文档的数组:

{
    _id: 'assignment_id_here',
    name: 'Homework Assignment 1',
    grades: [
        {
            _id: 'id_of_student_1',             
            grade: 88
        },
        {
            _id: 'id_of_student_2',             
            grade: 93
        },
        {
            _id: 'id_of_student_3',             
            grade: 76
        }
    ]    
}

然后您可以使用Handlebars帮助程序来显示嵌入的文档。以下示例演示了此(未经测试):

<强> HTML:

<head>
    <title>Assignment Example</title>
</head>

<body>
    {{> course}}
</body>

<template name="course">
    <ul>
    {{#each assignments}}
        <li>
            <p>Assignment Name: {{name}}</p>
            <p>Grades:
                {{#display}}    
                    <li>Name : {{name}}</li>
                    <li>Grade : {{grade}}</li>
                {{/display}}
            </p>
        </li>
    {{/each}}
    </ul>
</template>

JavaScript:

Assignments = new Meteor.Collection("assignments");
Students = new Meteor.Collection("students");

if (Meteor.isClient) {  
    Handlebars.registerHelper('display', function(context, options) {
        var el = "<ul>";
        for(var i=0, j=context.length; i<j; i++) {
            var student = Students.findOne({ "_id": context[i] }),
                assignment = Assignments.findOne(
                   { "grades._id": context[i] }, 
                   { "grades": { 
                       $elemMatch: { _id: context[i] } 
                      } 
                   });
            if (student && assignment){
                el += options.fn({name: student.name, grade: assignment.grades[0].grade});
            }
        }
        el += "</ul>"
        return el;
    });

    Template.course.assignments = function(){
        return Assignments.find({});
    }

    var student_one   = Students.insert({ name: "Student1" }),
        student_two   = Students.insert({ name: "Student1" }),
        student_seven = Students.insert({ name: "Student7" }),
        one     = { _id: student_one, grade: 88 },
        two     = { _id: student_two, grade: 93 },
        seven   = { _id: student_seven, grade: 76 });

    Assignments.insert({
        name: "Homework Assignment 1",
        grades: [one, two, three]
    }); 
}

if (Meteor.isServer) {}

答案 1 :(得分:0)

以下是两个选项:

你可以写一个帮手:

Template.registerHelper('getGrade', function (student) {
  return this.data.assignment.grades[student]
})

或者将成绩纳入学生的文件