在EmberJS 1.0.0-pre.4中检测路由转换 [英] Detect route transitions in EmberJS 1.0.0-pre.4
问题描述
我正在尝试检测何时发生路由转换。我将代码片段放在处理转换的最新版本的Ember( v1.0.0-pre.4
)中:
I am trying to detect when a route transition occurs. I've located this code snippet inside the latest version of Ember (v1.0.0-pre.4
) that handles the 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 + "'");
}
},
我将Ember应用程序设置为 window.App = Ember.Application.create()
。
I set up my Ember application as window.App = Ember.Application.create()
.
我注意到它调用这个。 notifyPropertyChange('url');
,但是我试图附加一个观察者到我的应用程序 App.Router
或 App。 Router.router
,我收到一个错误,因为它没有实现 Ember.Observable
。
I noticed that it calls this.notifyPropertyChange('url');
, but I tried to attach an observer to my application App.Router
or App.Router.router
, I get an error because it does not implement Ember.Observable
.
如何检测路由路径何时更改,而不为每个路由创建一个特殊的Ember.Route?
How can I detect when the route path is changed without creating a special Ember.Route for each of my routes?
推荐答案
更新的答案
现在,您可以直接在路由器的 didTransition
事件上绑定观察者:
You can now simply bind an observer on the router's didTransition
event:
App.Router.reopen({
doSomethingOnUrlChange: function() {
console.log(this.get('url'));
}.on('didTransition')
});
请参阅工作示例: http://jsfiddle.net/Sly7/3THD7/
下面的DEPRECATED ANSWER
在这里的代码片段中,有 set(appController,'currentPath',path);
我想你可以放一个
In the snippet here, there is set(appController, 'currentPath', path);
I think you can put an observer on this property.
我不知道您想要通知的位置,但可以在ApplicationController本身中执行。
I don't know exactly where you want to be notified, but it's possible to do it in the ApplicationController itself.
App.ApplicationController = Ember.Controller.extend({
currentPathDidChange: function(){
// the currentPath has changed;
}.observes('currentPath');
});
看到这个工作小提琴,例如: http://jsfiddle.net/qKrwU/
See this working fiddle for example: http://jsfiddle.net/qKrwU/
这篇关于在EmberJS 1.0.0-pre.4中检测路由转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!