2015-01-04 79 views
0

我想加載流星模板使用鐵路由器,但我加載的模板需要動態,我嘗試了幾種不同的方法,但都沒有工作。基於動態名稱的流星鐵路路由器加載模板

我的路由器

Router.route('/forms/add-form/:template', { 
    name: 'addForm', 
    layoutTemplate: 'layoutApp', 
    waitOn: function() { 
    return Meteor.subscribe('producersList'); 
    }, 
    data: function() { 
    return Producers.find(); 
    } 
}); 

路由器去

Router.go('addForm', {template: this.template}); 

的網址是好的,現在我第一次嘗試在我的路由器

name: this.params.template, 

具有這種但是,這並不工作,我現在在我的addForm模板中嘗試以下內容

{{> UI.dynamic template=formToLoad data=myDataContext}} 

formToLoad:function(){ 
    console.log('aaaa ' + this.template); 
} 
}); 

回答

2

您可以將數據傳遞到模板路線:

Router.route('/forms/add-form/:template', { 
    name: 'addForm', 
    layoutTemplate: 'layoutApp', 
    waitOn: function() { 
    return Meteor.subscribe('producersList'); 
    }, 
    data: function() { 
    return { 
     producers: Producers.find(), 
     formToLoad: this.params.template //here pass the template name 
    } 
    } 
}); 

,並在您的模板:

<template name="addForm"> 
    {{> Template.dynamic template=formToLoad}} 
</template> 

現在,如果我們運行:

Router.go('addForm', {template: 'someTemplateName'}); 

應該加載模板,名稱'someTemplateName'。對模板名稱使用camelCase語法,因爲當您將爲模板定義助手或事件時,您將得到'some-template-name'的語法錯誤。

+0

謝謝有趣的是在我的代碼中的其他地方做同樣的事情。 –