【问题标题】:Meteor Iron Router. One Route, Multiple Templates. How?流星铁路由器。一条路线,多个模板。如何?
【发布时间】: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}}

dynamic templates

路线如下:

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


【解决方案1】:

我会做你的主要模板想法,或者动态模板。

当您有很多可以动态配置的选项时,动态模板往往会更好。

但是当你只有几个选择时,我认为主要模板最终会更加明显。

如果您认为需要另一种选择,任何一种方式都可以轻松转换为另一种方式。

【讨论】:

    猜你喜欢
    • 2016-01-22
    • 2014-09-03
    • 2014-04-30
    • 1970-01-01
    • 1970-01-01
    • 2014-10-30
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多