流星中的4级订阅嵌套 [英] 4 level subscription nesting in meteor

查看:64
本文介绍了流星中的4级订阅嵌套的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用流星,这是我的模式,每个都是一个单独的集合:

I am using meteor and this is my schema, each is a separate collection:

课程有很多讲座

讲座有很多问题

问题有很多答案

我需要1页,可以显示给定课程的讲座,问题和答案.我可以毫无问题地显示课程的讲座,但是在显示更多嵌套项目时遇到了问题.我理想地希望拥有:

I want 1 page where I can display a given course's lectures, questions, and answers. I can display a course's lectures no problem but I have issues with displaying further nested items. I'd ideally like to have:

讲座有courseId

Lecture has courseId

答案中有讲义ID(但没有CourseId)

Answer has lectureId (but not courseId)

问题具有answerId(但没有演讲ID或CourseId)

Question has answerId (but not lectureId or courseId)

是明智的做法还是应该在所有子组件中都嵌入courseIds和LessonIds?这是我的主要路由器,我试图扩展与嵌套讲课有问题的想法相同的想法,但是我遇到了一个障碍,如何为订阅讲课ID提供订阅:

Is that wise or should I embed courseIds and lectureIds in all child components? This is my iron router, I tried to extend the same idea that worked with nesting lectures with questions but I hit a stumbling block with how to feed the subscriptions the lecturesId:

Router.route('/courses/:_id', {
    name: 'CoursePage',
    waitOn: function(){
        return [
            Meteor.subscribe('singleCourse', this.params._id),
            Meteor.subscribe('lectures', this.params._id),
            Meteor.subscribe('questions', this.params._id)
        ];
    },
    data: function() {
        return Courses.findOne(this.params._id);
    }
});

这是课程页面的订阅,这也是我的绊脚石,即我并不真正知道如何输入LessonId:

This is the subscriptions for the course page, again with my stumbling block of not really knowing how to feed in a lectureId:

Template.CoursePage.helpers({
    Lectures: function() {
        return Lectures.find({courseId: this._id});
    },
    Questions: function(lectureId) {
        return Questions.find({courseId: this._id, lectureId: lectureId});
    }
});

有人可以推荐一种在单个页面上进行4级嵌套的好方法吗?我认为我遗漏了一些明显的东西,但是我在Google搜索中找不到很好的例子.

Can anyone recommend a good way to do this 4 level nesting for a single page? I think that I am missing something obvious but I can't quite find a good example with google searching.

谢谢!

推荐答案

您可以发布复合材料包装为此.请参阅以下示例代码,并根据您的收集模式进行编辑,

You can Publish Composite package for this. See the following sample code and edit as per your collection schemas,

Meteor.publishComposite('singleCourse', function (courseId) {
    return [{
        find: function() {
            return Courses.find({ id: courseId});
        }
    }, {
        find: function() {
            return Lectures.find({ courseId: courseId});
        },
        children: [{
            find: function(lecture) {
                return Questions.find({ lectureId: lecture.id });
            },
            children: [{
                find: function(question) {
                   return Answers.find({ questionId: question.id });
                }
            }]
        }}
    }]
});

然后在路由器中,您只需拨打一个订阅电话,

Then in your router, you can simply make one subscription call,

Router.route('/courses/:_id', {
    name: 'CoursePage',
    waitOn: function(){
        return [
            Meteor.subscribe('singleCourse', this.params._id)
        ];
    },
    data: function() {
        return Courses.findOne(this.params._id);
    }
});

这是到目前为止最好的软件包之一(如果不是最好的话),以反应性地发布来自不同馆藏的相关文档集.

This is one of the best packages (if not the best) as of now to reactively publish set of related documents from different collections.

进行此类反应性连接时存在一些已知问题,但是对于较小的数据集,这毫无问题.

There are some known issues while doing these kind of reactive joins but for smaller datasets, this works without any problem.

希望有帮助.

这篇关于流星中的4级订阅嵌套的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆