【问题标题】:Multiple nested routes in Ember.js having weird behaviourEmber.js 中的多个嵌套路由具有奇怪的行为
【发布时间】:2014-06-11 01:05:48
【问题描述】:

我从 Ember.js 开始,其中一个页面有一个三级路由。这是路由器地图的样子:

App.Router.map(function(){
    this.resource('tests');
    this.resource('create', function() {
        this.resource('create.questions', {path: ':test_id' }, function() {
            this.resource('create.questions.question', {path: ':question_id'});
        });
    });
});

在我的 CreateRoute 中,我使用以下代码转换到 create/questions 路由:

this.get('controller').transitionToRoute('create/questions', test);

这很好用,但在我的 CreateQuestionsRoute 中,此代码不起作用:

this.get('controller').transitionToRoute('create/questions/question', question);

收到的错误:

Uncaught Error: Assertion Failed: Error: Assertion Failed: The route create/questions/question was not found 

使用 Chrome Ember 检查器插件,我可以看到路由是这样列出的:

CreateRoute
CreateQuestionsRoute
CreateQuestions.QuestionRoute

这似乎是任意行为。关于如何处理多个嵌套路由没有太多指导。一些参考资料告诉我,我的路线图实际上应该是这样的:

App.Router.map(function(){
        this.resource('tests');
        this.resource('create', function() {
            this.resource('questions', {path: ':test_id' }, function() {
                this.resource('question', {path: ':question_id'});
            });
        });
    });

路由名称会自动嵌套(不需要点符号),但这不起作用。任何有 Ember 智慧的人都可以为我发光吗?

【问题讨论】:

    标签: ember.js


    【解决方案1】:

    去吧:

    App.Router.map(function(){
        this.resource('tests');
        this.resource('create', function() {
            this.resource('questions', {path: ':test_id' }, function() {
                this.resource('question', {path: ':question_id'});
            });
        });
    });
    

    添加命名空间资源的唯一原因是资源不是唯一的。这意味着您可以使用任何路线

    this.transitionTo('questions', model);
    
    this.transitionTo('question', modelForQuestions, modelForQuestion);
    

    示例:http://emberjs.jsbin.com/OxIDiVU/636/edit

    如果你想保留你的命名空间,我会使用 camelCase 而不是点表示法,因为通常点表示当前范围内的属性。

    示例:http://emberjs.jsbin.com/OxIDiVU/637/edit

    【讨论】:

    • 感谢您的回答!但是,我想问一下是否有办法保留命名空间?如果我删除点符号,我将必须有一个 QuestionController,稍后我可能需要在不同的父路由下使用它。希望这是有道理的?
    • 我最终将需要单独的父级下的问题和问题资源。我知道“创建”听起来更像是一条路线,但 Ember 不允许在路线下嵌套
    • 是的,你完全可以做到,我会避免使用 dot 并使用 camelCase。 emberjs.jsbin.com/OxIDiVU/637/edit
    猜你喜欢
    • 2013-07-20
    • 2013-01-16
    • 1970-01-01
    • 2013-11-04
    • 2012-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-18
    相关资源
    最近更新 更多