How Can The Current Route Be Observed for Changes?
Solution 1
So the properties I'm trying to work with belong on the Controller for the route, not the View.
Given some contactUs
route, it's Controller might look like
App.ContactUsController = App.AppController.extend({
title: "Contact Us"
});
My App.ApplicationController
can now look like
App.ApplicationController = Ember.Controller.extend({
updateTitle: function() {
window.document.title = this.controllerFor(this.currentPath).get('title');
}.observes('currentPath')
});
So the API methods/properties I was looking for are
App.ApplicationController.get('currentPath')
(currently appears to be undocumented on the site)controllerFor
(also seems undocumented, but is found within areopen
implementation forEmber.ControllerMixin
)
Solution 2
- What do I set the Observer to observe? I'm having trouble finding a currentState property or equivalent to observe.
You can observe the ApplicationController.currentPath
. For example:
App.ApplicationController = Ember.Controller.extend({
currentPathDidChange: function() {
path = this.get('currentPath');
console.log('path changed to: ', path);
window.document.title = path;
}.observes('currentPath')
});
- Within the Observer, how can I access the App.AboutView associated with the route?
Trying to access App.AboutView
from this observer would be tricky and probably not a good idea. I'm not clear on why a view's property would be useful in this scenario. Can you elaborate?
deefour
My name is Jason. I am a full stack web developer. I like the internet.
Updated on June 06, 2022Comments
-
deefour almost 2 years
I want to update the
<title>
tag for the page whenever the location (route) changes. I'm particularly interested in observingApp.Router
's current route changing - I then want to access the View associated with that route and update the<title>
tag from the observer based on a property of the View.For example, if going to
/about
- What do I set the Observer to observe? I'm having trouble finding a
currentState
property or equivalent to observe. - Within the Observer, how can I access the
App.AboutView
associated with the route?
I'm using
1.0.0pre4
My goal is to have a
title
property and anauthorization
property on each View that the Observer oncurrentPath
will work with. I want to use thetitle
property to update thedocument.title
, and theauthorization
property object to check wither my current user can access the route.Now that I say this and with @Michael Grassotti's answer, perhaps these properties belong on the Controller and not the View. The goal is to gain access to these properties associated with the current Route's context to modify the
document.title
and check whether or not myApp.CurrentUserController
(that stores aUser
object model for the logged in user) is authorized to acess the current route. - What do I set the Observer to observe? I'm having trouble finding a
-
deefour over 11 yearsThanks for the reply; I updated my Question to give more context.
-
deefour over 11 yearsI have sorted out the other half of the problem. Thanks for the nudge with
currentPath
. -
Mike Grassotti over 11 yearsNo problem. Totally agreed re: your conclusion above, these properties belong on the controller.
-
Sam Selikoff almost 11 yearsCan you do something like this on a view?
-
Mike Grassotti almost 11 yearsSure. From applicationView it would be
observes('controller.currentPath')
-
Zanderi almost 10 yearsIm setting a property in my controller whenever the route changes it gets updated. However the issue is that on a dynamic segment it doesn't change the route so currentPath won't work. I need to observe this change in order to update this property. The closest I have come is this stackoverflow.com/questions/16471068/… However if I place App.Router.router.generate(['posts.new']); in the observer it still doesn't fire. Any ideas?
-
SuperUberDuper about 9 yearswould it be possible for a component to observe this route change without extending the ApplicationController?
-
SuperUberDuper about 9 yearsIf I create this controller in an addons app folder, how can I get it to merge with the ember cli apps own appController?
-
Rob Dawson almost 9 yearsI couldn't get this to work with Ember 1.13, but found that the reopening the router and observing the on('didTransition') event as described here worked: stackoverflow.com/a/27825139/333068