dee*_*our 15 ember.js ember-router
我想<title>在位置(路线)发生变化时更新页面的标签.我对观察App.Router当前路径更改特别感兴趣- 然后我想访问与该路由关联的View,并<title>根据View的属性更新观察者的标签.
例如,如果去 /about
currentState属性或等同物来观察.App.AboutView与路由关联的?我正在使用 1.0.0pre4
我的目标是在Observer on 将使用的每个View上拥有一个title属性和一个authorization属性currentPath.我想使用该title属性来更新document.title,并使用authorization属性对象来检查当前用户是否可以访问该路由.
现在我说这个和@Michael Grassotti的答案,也许这些属性属于Controller而不是View.目标是访问与当前Route的上下文关联的这些属性,以修改document.title并检查我App.CurrentUserController (存储User登录用户的对象模型)是否被授权访问当前路由.
Mik*_*tti 16
- 我该如何设置要观察的观察者?我无法找到currentState属性或等效于观察.
你可以观察一下ApplicationController.currentPath.例如:
App.ApplicationController = Ember.Controller.extend({
currentPathDidChange: function() {
path = this.get('currentPath');
console.log('path changed to: ', path);
window.document.title = path;
}.observes('currentPath')
});
Run Code Online (Sandbox Code Playgroud)
- 在Observer中,如何访问与路由关联的App.AboutView?
试图App.AboutView从这个观察者访问将是棘手的,可能不是一个好主意.我不清楚为什么视图的属性在这种情况下会有用.你能详细说说吗?
因此,我正在尝试使用的属性属于Controller的路由,而不是View.
给定一些contactUs路线,它的控制器可能看起来像
App.ContactUsController = App.AppController.extend({
title: "Contact Us"
});
Run Code Online (Sandbox Code Playgroud)
我App.ApplicationController现在可以看起来像
App.ApplicationController = Ember.Controller.extend({
updateTitle: function() {
window.document.title = this.controllerFor(this.currentPath).get('title');
}.observes('currentPath')
});
Run Code Online (Sandbox Code Playgroud)
所以我正在寻找的API方法/属性是
App.ApplicationController.get('currentPath') (目前似乎在网站上没有记录)controllerFor (也似乎没有文档,但在reopen实现中找到Ember.ControllerMixin)