我试图让Ember指南中的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>
据我所知,当我到达索引页面时,它应该显示标题,但没有任何反应。所以有些人可能会看到这里的错误。
答案 0 :(得分:1)
您在title
中设置了IndexController
属性,但您的模板似乎不是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)
答案 1 :(得分:0)
好的,这个小句似乎在这里很重要:
IndexController是索引模板的起始上下文
所以{{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>