Route transition detection in EmberJS 1.0.0-pre.4

I am trying to detect when a route transition occurs. I found this piece of code inside the latest version of Ember ( v1.0.0-pre.4 ) that handles transitions:

  didTransition: function(infos) { // Don't do any further action here if we redirected if (infos[infos.length-1].handler.transitioned) { return; } var appController = this.container.lookup('controller:application'), path = routePath(infos); set(appController, 'currentPath', path); this.notifyPropertyChange('url'); if (get(this, 'namespace').LOG_TRANSITIONS) { Ember.Logger.log("Transitioned into '" + path + "'"); } }, 

I installed the Ember application as window.App = Ember.Application.create() .

I noticed that it calls this.notifyPropertyChange('url'); , but I tried to connect the observer to my App.Router or App.Router.router , I get an error because it does not implement Ember.Observable .

How do I determine when a route has been changed without creating a special Ember.Route for each of my routes?

+2
source share
1 answer

UPDATED RESPONSE

Now you can simply bind the observer to the didTransition router didTransition :

 App.Router.reopen({ doSomethingOnUrlChange: function() { console.log(this.get('url')); }.on('didTransition') }); 

see working example: http://jsfiddle.net/Sly7/3THD7/

CUT RESPONSE BELOW

This snippet has set(appController, 'currentPath', path); I think you can put an observer in this property.

I do not know exactly where you want to receive notifications, but it is possible to do this in the ApplicationController itself.

 App.ApplicationController = Ember.Controller.extend({ currentPathDidChange: function(){ // the currentPath has changed; }.observes('currentPath'); }); 

See this working script, for example: http://jsfiddle.net/qKrwU/

+9
source

Source: https://habr.com/ru/post/1480891/


All Articles