【问题标题】:Handling page header in ember.js在 ember.js 中处理页眉
【发布时间】:2014-05-27 19:56:23
【问题描述】:

我正在尝试使用 Ember.js 制作一个简单的应用程序。我正在使用yeoman ember

我有以下应用程序模板

<h1>Dashboard</h1>
<div class="content">{{outlet}}</div>

我定义了几条路线。现在我无法做的是,找到一种方法来更新 &lt;h1&gt; 内的文本以获取不同的路线。例如,

如果我导航到/#/users,我需要将其更改为Users。 如果我导航到/#/users/1,我需要将其更改为模型的名字。

我在 ApplicationRoute 中试过这个

setupController: function(controller){
    controller.set('title', 'Dashboard');
} 

并更改为&lt;h1&gt;{{title}}&lt;/h1&gt; 但它只适用于ApplicationRoute,没有其他路线!

setupController: function(controller){
    controller.set('title', 'Users');
} 

这在 UsersRoute 中不起作用。连路由的输出都消失了。

什么是让它工作的正确方法!

【问题讨论】:

    标签: javascript ember.js yeoman


    【解决方案1】:

    您如何简单地在 ApplicationRoute 中创建一个操作,将标题设置为作为参数传递给它,然后让您的子路由在 setupController 中调用该操作。这应该够了吧。 所以在你的申请路线中:

    //in application route 
    actions:{
        updatePageTitle: function(title){
            this.controllerFor('application').set('title', title);
        }
    }
    

    在您的个人子路线中:

    setupController:function(controller, model){
        this.send('updatePageTitle', model.get('something'));
        this._super(controller, model);
    }
    

    【讨论】:

    • 标题成功了。但是在用户路径中添加setupController,会使所有模型消失!!
    • 更新了我的答案,您还必须在 setupController 中调用 _super 或者手动将模型设置为控制器。希望这能解决您的问题。
    • hmmm.. 现在它工作了.. 对于这么小的要求来说似乎太多了! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-25
    • 2013-12-07
    • 1970-01-01
    • 2015-03-20
    • 1970-01-01
    • 2014-02-26
    • 2013-08-20
    相关资源
    最近更新 更多