流星铁路由器。一条路线,多个模板。如何?
Meteor Iron Router. One Route, Multiple Templates. How?
我正在使用最新的 Meteor 和 Iron Router。想象一下有一个 'customComputer' 集合。计算机有三种不同的状态:'ordering'、'building'、'shipped'.
现在,我为此使用了三种不同的路由,每一种都有不同的模板
/o/_id
/b/_id
/s/_id
一台计算机不能同时处于两种状态,所以我想有一条路线。我该如何处理模板?
/c/_id
我能想到的最好办法是制作一个 "main" 模板,链接到其他模板。这是最佳做法吗?
{{#if isOrder}}{{>orderTemplate}}{{/if}}
{{#if isBuilding}}{{>buildingTemplate}}{{/if}}
{{#if isShipped}}{{>shippedTemplate}}{{/if}}
路线如下:
Router.route('order', {
path: '/o/:b/:computerId',
onAfterAction: function() {
if (this.title) document.title = this.title;
},
data: function() {
if(!this.ready()) return;
var o = Computer.findOne(this.params.computerId);
if(!o) throw new Meteor.Error('Computer not found');
return o;
},
waitOn: function() {
if (!Meteor.userId()) return this.next(); // Don't subscribe if not logged in.
return [
Meteor.subscribe('computerById', this.params.computerId),
Meteor.subscribe('myProfile'),
];
},
});
有没有更好的方法?
我会做你的主要模板创意,或者动态模板。
当您有很多可以动态配置的选项时,动态模板往往会更好。
但我认为当你只有几个选择时,主要模板最终会更加明显。
如果您认为需要其他选项,可以轻松地将任何一种方式转换为另一种方式。
我正在使用最新的 Meteor 和 Iron Router。想象一下有一个 'customComputer' 集合。计算机有三种不同的状态:'ordering'、'building'、'shipped'.
现在,我为此使用了三种不同的路由,每一种都有不同的模板
/o/_id
/b/_id
/s/_id
一台计算机不能同时处于两种状态,所以我想有一条路线。我该如何处理模板?
/c/_id
我能想到的最好办法是制作一个 "main" 模板,链接到其他模板。这是最佳做法吗?
{{#if isOrder}}{{>orderTemplate}}{{/if}}
{{#if isBuilding}}{{>buildingTemplate}}{{/if}}
{{#if isShipped}}{{>shippedTemplate}}{{/if}}
路线如下:
Router.route('order', {
path: '/o/:b/:computerId',
onAfterAction: function() {
if (this.title) document.title = this.title;
},
data: function() {
if(!this.ready()) return;
var o = Computer.findOne(this.params.computerId);
if(!o) throw new Meteor.Error('Computer not found');
return o;
},
waitOn: function() {
if (!Meteor.userId()) return this.next(); // Don't subscribe if not logged in.
return [
Meteor.subscribe('computerById', this.params.computerId),
Meteor.subscribe('myProfile'),
];
},
});
有没有更好的方法?
我会做你的主要模板创意,或者动态模板。
当您有很多可以动态配置的选项时,动态模板往往会更好。
但我认为当你只有几个选择时,主要模板最终会更加明显。
如果您认为需要其他选项,可以轻松地将任何一种方式转换为另一种方式。