Ember.js 控制器。在IndexRoute中设置

Ember.js 控制器。在IndexRoute中设置,ember.js,Ember.js,我有以下js代码: App = Ember.Application.create({ LOG_TRANSITIONS: true }); App.Router.map(function() { this.resource('posts'); this.resource('about'); }); App.IndexRoute = Ember.Route.extend({ setupController: function(controller) {

我有以下js代码:

App = Ember.Application.create({
    LOG_TRANSITIONS: true
});

App.Router.map(function() {
    this.resource('posts');
    this.resource('about');
});

App.IndexRoute = Ember.Route.extend({
     setupController: function(controller) {
         controller.set('title', "test");
     }
});

App.PostsRoute = Ember.Route.extend({
    setupController: function(controller) {
        controller.set('title', 'test');
    }
});
以及以下html页面:

<script type="text/x-handlebars">
    <div class="container">
        <div class="navbar">
            <div class="navbar-inner">
                <ul class="nav">
                    <li>{{#linkTo index}}Home{{/linkTo}}</li>
                    <li>{{#linkTo posts}}Posts{{/linkTo}}</li>
                    <li>{{#linkTo about}}About{{/linkTo}}</li>
                </ul>
            </div>
        </div>
        {{title}} // <--- no title here
        {{outlet}}
    </div>
</script>

<script type="text/x-handlebars" id="about">
    About template
</script>

<script type="text/x-handlebars" id="posts">
    <h1>{{title}}</h1> // <--- works here
    Recent posts
</script>

  • {{{#链接到索引}}主页{{/linkTo}
  • {{{linkTo posts}}posts{{/linkTo}
  • {{{linkTo about}}关于{{/linkTo}

{{title}}//不是100%确定,但我认为您必须在
ApplicationRoute
中设置它。如果我使用ApplicationRoute,我会在任何地方呈现{{title},但这似乎有点混乱,因为如果我放置console.log('test')在setupController功能和导航栏中,如果使用ApplicationRoute,则只会收到一次消息,如果使用IndexRoute,则每次转到“/”时都会收到消息。好吧,继续和EmberJs抗争吧!非常感谢。