将控制器应用于 Meteor 中没有路由的模板

Apply controller to template without a route in Meteor

如果我有一个没有自己路由的模板,我该如何为其应用控制器?我有一个名为 Cart 的模板,它嵌入到另一个名为 Products:

的模板中
<template name="Products">
<div class="row">
    <div class="col-md-6 col-md-offset-6">
        {{> Cart}}
    </div>
</div>

我没有 www.myapp.com/cart 之类的路线。如果我使用类似的东西:

Router.route(, {name: 'Cart', controller: 'cartController});

我会得到一个错误。有人可以帮忙吗?谢谢!

正如控制台可能向您指出的那样,在 JS 中不能有逗号。看起来你的想法是,只是不传递到 route 的路由。要在 JS 中做到这一点,你必须做 Router.route(undefined, ...)。但顺便说一句..

在我看来,通过阅读 iron-router 指南,您可以 simply pass a template to the controller。这样他们也联系在一起了。

我复制了指南中的示例并进行了编辑以满足您的需要:

CartController = RouteController.extend({
  template: 'cart',
  action: function () {
    this.render();
  }
});