2013-04-25 206 views
0

我尝试从灰烬指南得到example的工作,但我不明白:灰烬路由器和控制器

window.App = Ember.Application.create() 

App.Router.map -> 
    this.route("about"); 
    this.route("favorites", { path: "/favs" }) 

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

模板:

<h1>{{title}}</h1> 
<nav> 
    {{#linkTo "index"}}Index{{/linkTo}} 
    {{#linkTo "about"}}About{{/linkTo}} 
    {{#linkTo "favorites"}}Favorites{{/linkTo}} 
</nav> 

据我了解的例子应该显示标题,当我到达索引页面,但没有任何反应。所以有人可以看到这里有什么不对。

回答

1

您正在设置IndexController中的title属性,但它看起来像您的临时迟到不是index模板,而是application模板。你可以改变你的IndexRoute使用controllerFor这样你就可以访问ApplicationController(自动生成),并设置一个值的属性,与此类似:

App.IndexRoute = Ember.Route.extend 
    setupController: (controller) -> 
    @controllerFor('application').set('title', 'My App') 

(见fiddle

或者你也可以做到这一点直接在ApplicationRoute

App.ApplicationRoute = Ember.Route.extend 
    setupController: (controller, model) -> 
    controller.set('title', 'My App') 

(见fiddle

0

OK了这个小句似乎是很重要的位置:

的索引控制器是该指数模板

所以{{title}}必须是在索引模板开始上下文:

script(type="text/x-handlebars") 

    <nav> 
    {{#linkTo "index"}}Index{{/linkTo}} 
    {{#linkTo "about"}}About{{/linkTo}} 
    {{#linkTo "favorites"}}Favorites{{/linkTo}} 
    </nav> 
    {{outlet}} 

script(type="text/x-handlebars", data-template-name="index") 
    <h1>{{title}}</h1>