2015-03-31 96 views
3

我想從鐵路由器數據中傳入categoryId到流星模板助手中。從鐵路由器傳遞數據到模板助手

這是我的路由器代碼:

Router.route('/lessons/:categoryId', function() { 
this.subscribe('lessons'); 
this.render('Lessons', { 
    data: { 
     categoryId: this.params.categoryId 
    } 
}); 

這是我的模板代碼:

Template.Lessons.helpers({ 
lessons: function() { 
console.log('CategoryId: '+categoryId); 
} 
}); 

我該如何正確訪問是在鐵路由器創建的categoryId?

非常感謝您的幫助。

回答

4

data從您的路由器爲您的模板提供上下文(this)。從您的助手來訪問categoryId,用this.categoryId

Template.Lessons.helpers({ 
    lessons: function() { 
    console.log('CategoryId: ' + this.categoryId); 
    } 
}); 

您還可以通過接入路由器的數據:

Template.instance().data.categoryId; 
+0

感謝這麼多的工作。我在我的代碼中遇到了另一個小問題,這使我無法實現這個.classId在我第一次嘗試時就已經工作了。如果有其他人遇到此問題,我會在這裏發帖。當我嘗試使用它時,它正在讀取我的categoryId作爲字符串而不是數字。修復:var categoryId = parseInt(this.categoryId); var lessonsData = Lessons.find({categoryId:categoryId})。fetch(); – 2015-03-31 16:53:25

相關問題