4

我刚才在 ember-cli 中更改了我的路由结构并破坏了我的应用程序。我想将我当前的结构嵌套更深一层......

前:

Router.map(function() {
    this.resource('placements', function() {
        this.route('add');
        this.route('import');
        this.route('open');
    });
    this.route('admin');
});

后:

Router.map(function() {
    this.resource('portal', function() {
        this.resource('placements', function() {
            this.route('add');
            this.route('import');
            this.route('open');
        });
        this.route('admin');
    });
});

我的模板结构也需要改变......

前:

- templates/
-     placements/
-         add.hbs
-         import.hbs
-         index.hbs
-         open.hbs
-     admin.hbs
-     application.hbs
-     index.hbs
-     placements.hbs

后:

- templates/
-     portal/
-         placements/
-             add.hbs
-             import.hbs
-             index.hbs
-             open.hbs
-         admin.hbs
-         index.hbs
-         placements.hbs
-     application.hbs
-     index.hbs
-     portal.hbs

我以为我已经进行了一对一的所有更改,但是由于某种原因,当我重新启动 ember 服务器时,嵌套的“放置”路线被破坏了。

控制台日志中,我注意到 ember 仍在尝试placements.index在旧目录下templates/placements/index而不是新目录下查找。

有一种理论认为模板不能像路由器那样嵌套?这意味着我可能需要用renderTemplate钩子显式定义每条路线,以便它显示正确的模板......这可能会很痛苦,因为这个项目将会非常大......也许还有别的东西我做错了吗?

4

1 回答 1

9

在处理@Adam 离开我的评论之后。我能够找出使用模板的正确方法。

模板确实不嵌套在与资源/路由完全相同的模式中。

鉴于我制作的新路线结构:

Router.map(function() {
    this.resource('portal', function() {
        this.resource('placements', function() {
            this.route('add');
            this.route('import');
            this.route('open');
        });
        this.route('admin');
    });
});

模板的结构应如下所示:

- templates/
-     placements/
-         add.hbs
-         import.hbs
-         index.hbs
-         open.hbs
-     portal/
-         admin.hbs
-         index.hbs
-     application.hbs
-     index.hbs
-     placements.hbs
-     portal.hbs

当您定义“资源”时,这实质上会导致该特定路由成为顶级对象,无论它在路由器映射中嵌套的深度如何。 placements.hbsportal.hbs(从上面)是“资源”(顶层)模板,并相应地放置在模板目录的顶层。

由于这两个“资源”模板每个都有一个“子”路由模板,因此需要将其放置在以其相应资源命名{{outlet}}的顶级目录中。

Ember-CLI 文档描述了这个模式,只展示了一个深度的例子,所以我首先假设模式重复深入。我希望这有助于阐明学习 Ember 的其他人的实际方法。


更新:

从 EMBER 1.7.0 开始

路线现在是可嵌套的!

这对我个人来说是令人兴奋的消息,但值得对此答案进行更新。

资源仍然可以用作上面的示例,因此如果您对此感到满意,则可以继续这样做一段时间。但请记住,资源计划用于最终弃用路径。最终,只会有路由和嵌套路由。

好消息是,如果您遵循新模式,您可以更自然地嵌套模板以匹配您的路由器结构,就像自然 REST 风格的应用程序一样。不再有尴尬的心理映射。

例如上面的同一个路由器(略有变化):

Router.map(function() {
    this.route('portal', function() {
        this.route('placements', function() {
            this.route('add');
            this.route('import');
            this.route('open');
        });
        this.route('admin');
    });
});

模板的结构可以如下:

- templates/
-     portal/
-         placements/
-             add.hbs
-             import.hbs
-             index.hbs
-             open.hbs
-         admin.hbs
-         index.hbs
-         placements.hbs
-     application.hbs
-     index.hbs
-     portal.hbs

相同的模式适用于嵌套相应的 /routes、/controllers、/adapter 等。

这也意味着(使用这种新模式){{link-to}}助手和transitionTo方法将需要包含路由的完整路径:例如 {{link-to 'portal.placements.index'}}.

于 2014-07-03T20:32:39.977 回答