2015-04-23 69 views
2

我的应用程序中有几条路径看起来如下,全都在面包屑路径下。面包屑路径:检索路径下所有东西的数据

/class/:slug 
/class/:slug/student/:_id 
/class/:slug/teacher/:_id 

然而,这往往会导致冗余,因为我必须不断查找由塞类查询

this.route('Class.teacher', { 
    path: '/class/:slug/teacher/:_id', 
    waitOn: function() { 
    return Meteor.subscribe('teachers'); 
    }, 
    data: function() { 
    var classId = Classes.findOne({ slug: this.params.slug })._id; 
    if(!classId) { 
     throw new Meteor.Error(404, 'That class does not exist'); 
    } 

    return Teacher.findOne(
     { _id: this.params._id, classIds: classId } 
    ); 
    } 
}) 

问题之前,冗余的,这似乎引起金额,我开始我大部分路线都是找到班级的。

有没有办法对/class/:slug路线进行某种操作?我在想这样的事情:

Router.route('/class/:slug/*', { 
    waitOn: function() { 
    return Meteor.subscribe('classes', this.params.slug); 
    }, 
    onBeforeAction: function() { 
    var class = Classes.findOne(this.params.slug); 
    if(!class) { 
     throw new Meteor.Error(404, "Class not found"); 
     this.stop(); 
    } 
    this.next(); 
    } 
}); 

但是,这是行不通的。以这种方式处理面包屑路径的正确方法是什么?

回答

0

你很可能

Router.onBeforeAction(function(req, res, next){ 
    // find class here 
    // req.params.slug Not sure about this 
    next() 
}, { only: ['class'] }); 

只是一个想法,但。