【发布时间】:2015-12-12 01:00:32
【问题描述】:
我正在使用最新的 Meteor 和 Iron 路由器。想象一下有一个“customComputer”集合。计算机具有三种不同的状态:'ordering'、'building'、'shipped'。
现在,我为此使用了三种不同的路线,每条路线都有不同的模板
/o/_id
/b/_id
/s/_id
一台计算机不能同时处于 2 个状态,所以我想要一个路由。如何处理模板?
/c/_id
我能想到的最好办法是制作一个链接到其他模板的“主”模板。这是最佳做法吗?
{{#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'),
];
},
});
有没有更好的办法?
【问题讨论】:
-
你想出的最好的在我看来是合理的。使用这种方法有什么不符合您的要求吗?
-
谢谢,我实现了这两个并选择了动态模板。这感觉像是一个错误的地方,但没什么大不了的。我担心同时加载所有三个模板的性能,但这似乎是我们的做法:-)
-
两种实现之间的性能差异有多大?鉴于只有一个 if 语句为真,您仍然只加载和呈现一个模板。我预计两者之间不会有任何显着的性能差异。
-
@JeremyK 他们看起来是一样的,但我并没有强调他们的表现。
标签: meteor iron-router meteor-blaze