Ember - 在控制器或路由中设置主页标题? [英] Ember - Setting homepage title in controller or route?

查看:119
本文介绍了Ember - 在控制器或路由中设置主页标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是我的HTML:

 < script type =text / x-handlebars> 
< div id =top-panel>
< h1> {{title}}< / h1>
< / div>
< div id =wrapper>
< div id =content>
{{outlet}}
< / div>
< / div>
< / script>

我应该在哪里设置{{title}}?我发现有两种方法可以做到这一点...



首先:

  App.ApplicationRoute = Ember.Route.extend({
setupController:function(controller){
controller.set(title,Foo Bar)
}
});

第二:

 code> App.ApplicationController = Ember.ObjectController.extend({
title:Hello World
});

哪一种是推荐的方式?

解决方案

如果这只是一个永远不会改变的静态事物,我会把它放在第二个,或者只是在把手模板中。



另外


  1. 由于您没有在应用程序控制器上设置对象,需要扩展ObjectController,可以扩展Controller。

      Ember.ObjectController是Ember的Controller层的一部分。它旨在
    包装单个对象,代理未处理的尝试来获取和设置
    底层内容对象,并将未处理的操作尝试转发到其目标。


  2. 将来,如果您重写setupController,通常建议您调用this._super (控制器,模型)如:

      setupController:function(controller,model){
    this._super(controller,模型)
    controller.set(title,Foo Bar)
    }


setupController的默认实现是设置控制器上的内容,所以技术上没有什么不同,你没有调用超级。



在动态标题的情况下,我会将其设为计算属性:

  App.ApplicationController = Ember.ObjectController.extend({
title:function(){
var currentdate = new Date(),
title =Awesome Title:+ currentdate.getDate()+/
+(currentdate.getMonth()+ 1)+/
+ currentdate.getFullYear()+@
+ currentdate.getHour s()+:
+ currentdate.getMinutes()+:
+ currentdate.getSeconds();

返回标题;
} .property()
});


What is the best practice?

This is my HTML:

<script type="text/x-handlebars">
    <div id="top-panel">
        <h1>{{title}}</h1>
    </div>
    <div id="wrapper">
        <div id="content">
            {{outlet}}
        </div>
    </div>
</script>

Where should I set {{title}}? I found out there are two ways to do this...

First:

App.ApplicationRoute = Ember.Route.extend({
    setupController: function (controller) {
        controller.set("title", "Foo Bar")
    }
});

Second:

App.ApplicationController = Ember.ObjectController.extend({
    title: "Hello World"
});

Which one is a recommended way of doing this?

解决方案

If it's just a static thing that will never change, I'd put it in the second, or just in the handlebars template.

Additionally

  1. since you aren't setting an object on the application controller, it doesn't really need to extend the ObjectController, you can just extend Controller.

    Ember.ObjectController is part of Ember's Controller layer. It is intended to 
    wrap a single object, proxying unhandled attempts to get and set to the 
    underlying content object, and to forward unhandled action attempts to its target.
    

  2. In the future, if you are overriding the setupController it's usually recommended to call this._super(controller, model) like:

    setupController: function (controller, model) {
       this._super(controller, model)
       controller.set("title", "Foo Bar")
    }
    

setupController's default implementation is to set the content on the controller, so technically it makes no difference that you aren't calling super.

In the case of a dynamic title, I'd make it a computed property:

 App.ApplicationController = Ember.ObjectController.extend({
     title: function(){
        var currentdate = new Date(),
            title = "Awesome Title: " + currentdate.getDate() + "/"
            + (currentdate.getMonth()+1)  + "/" 
            + currentdate.getFullYear() + " @ "  
            + currentdate.getHours() + ":"  
            + currentdate.getMinutes() + ":" 
            + currentdate.getSeconds();

          return title;
     }.property()
 });

这篇关于Ember - 在控制器或路由中设置主页标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆