Meteor:Select 模板有条件地基于来自控制器的 url 参数
Meteor: Select template conditionally based on url argument from controller
嗨,我有这条路线
#CoffeeScript
Router.route 'tests/:slug/:type/question/add',
name: 'addQuestion'
controller: testsAddQuestionController
我有这个控制器
@testsAddQuestionController = testsQuestionsController.extend
template: 'mcqQuestionForm'
data: ->
questions: TestQuestions.find()
test: Tests.findOne slug: this.params.slug
我想根据 :type 参数的值从控制器中 select 模板,我尝试了两种方法:
@testsAddQuestionController = testsQuestionsController.extend
template: if this.params.type is 'mcq' then 'mcqQuestionForm' else 'somethingelse'
data: ->
questions: TestQuestions.find()
test: Tests.findOne slug: this.params.slug
但是用这种方法我得到了错误this.params is undefined
第二种方法
@testsAddQuestionController = testsQuestionsController.extend
template: if Router.current().route.params.type is 'mcq' then 'mcqQuestionForm' else 'somethingelse'
data: ->
questions: TestQuestions.find()
test: Tests.findOne slug: this.params.slug
但是使用这种方法应用程序崩溃了,有没有人知道如何访问路由参数以便从控制器为 select 模板创建条件?
这是我们的控制器的样子:
@testsAddQuestionController = testsQuestionsController.extend
template: ->
qType = Router.current().params.type
if qType == 'practical'
'practicalQuestionForm'
else if qType == 'mcq'
'mcqQuestionForm'
data: ->
questions: TestQuestions.find()
test: Tests.findOne slug: this.params.slug
出于某种原因(不知道为什么),如果我将当前 Router 参数保存到一个变量中,然后将该变量用于 if 语句,那么它就可以工作了。
希望对您有所帮助。
嗨,我有这条路线
#CoffeeScript
Router.route 'tests/:slug/:type/question/add',
name: 'addQuestion'
controller: testsAddQuestionController
我有这个控制器
@testsAddQuestionController = testsQuestionsController.extend
template: 'mcqQuestionForm'
data: ->
questions: TestQuestions.find()
test: Tests.findOne slug: this.params.slug
我想根据 :type 参数的值从控制器中 select 模板,我尝试了两种方法:
@testsAddQuestionController = testsQuestionsController.extend
template: if this.params.type is 'mcq' then 'mcqQuestionForm' else 'somethingelse'
data: ->
questions: TestQuestions.find()
test: Tests.findOne slug: this.params.slug
但是用这种方法我得到了错误this.params is undefined
第二种方法
@testsAddQuestionController = testsQuestionsController.extend
template: if Router.current().route.params.type is 'mcq' then 'mcqQuestionForm' else 'somethingelse'
data: ->
questions: TestQuestions.find()
test: Tests.findOne slug: this.params.slug
但是使用这种方法应用程序崩溃了,有没有人知道如何访问路由参数以便从控制器为 select 模板创建条件?
这是我们的控制器的样子:
@testsAddQuestionController = testsQuestionsController.extend
template: ->
qType = Router.current().params.type
if qType == 'practical'
'practicalQuestionForm'
else if qType == 'mcq'
'mcqQuestionForm'
data: ->
questions: TestQuestions.find()
test: Tests.findOne slug: this.params.slug
出于某种原因(不知道为什么),如果我将当前 Router 参数保存到一个变量中,然后将该变量用于 if 语句,那么它就可以工作了。
希望对您有所帮助。