在 EmberJS 1.0.0-pre.4 中检测路由转换 [英] Detect route transitions in EmberJS 1.0.0-pre.4

查看:16
本文介绍了在 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().

我注意到它调用了 this.notifyPropertyChange('url');,但我尝试将观察者附加到我的应用程序 App.RouterApp.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/

以下已弃用的答案

在这里的代码片段中,有 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆