在路由之间共享代码

时间:2013-07-30 12:20:46

标签: ember.js

为了在我的不同路线之间共享代码(它们大致相同),我尝试执行以下操作:

/// Base classes (not real routes)

App.RouteMixin = Ember.Mixin.create({
    ...
});

App.BaseIndexRoute = Ember.Route.extend(App.RouteMixin, {
    ...
});

/// NODES

App.rNodesMixin = Ember.Mixin.create({
    ...
});

App.NodesIndexRoute = App.BaseIndexRoute.extend(App.rNodesMixin, {
    ...
});

App.NodesShowRoute = App.BaseShowRoute.extend(App.rNodesMixin, {
    ...
});

App.NodesEditRoute = App.BaseEditRoute.extend(App.rNodesMixin, {
    ...
});

App.NodesNewRoute = App.BaseNewRoute.extend(App.rNodesMixin, {
    ...
});

/// AGENTS

App.rNodesMixin = Ember.Mixin.create({
    ...
});

App.AgentsIndexRoute = App.BaseIndexRoute.extend(App.rAgentsMixin, {
    ...
});

App.AgentsShowRoute = App.BaseShowRoute.extend(App.rAgentsMixin, {
    ...
});

App.AgentsEditRoute = App.BaseEditRoute.extend(App.rAgentsMixin, {
    ...
});

App.AgentsNewRoute = App.BaseNewRoute.extend(App.rAgentsMixin, {
    ...
});

我突然发现以下错误:

Assertion failed: The attempt to linkTo route 'nodes.index' failed. The router did not find 'nodes.index' in its possible routes: 'index'

相同的模式适用于控制器。使用mixin / extend?

是不可能重用路由的代码

1 个答案:

答案 0 :(得分:1)

我认为你的路由器映射有一些错误。我刚刚更改了路由器映射并且工作正常。 看看这个Jsfiddle

我使用了以下配置:

App.Router.map(function() {
    this.resource("nodes", function(){
        // other routes
    });
    this.resource("agents", function(){
        // other routes
    });
});