在EmberJS中检测URL更改并获取URL(使用Discourse)

kap*_*ron 4 ember.js ember-router ember-rails discourse

我正在使用基于EmberJS构建的Discourse(http://www.discourse.org/),并尝试随时观察URL的变化,例如在打开新主题时.我已经看到了观察currentPath的答案,例如: 在EmberJS 1.0.0-pre.4中检测路由转换

App.ApplicationController = Ember.Controller.extend({

  routeChanged: function(){
    // the currentPath has changed;
  }.observes('currentPath');
});
Run Code Online (Sandbox Code Playgroud)

但我正在尝试检测任何 URL更改,而不仅仅是路径更改.如该答案的评论中所述:

从例如转换/pages/1/pages/2因为路径保持不变时,此观察者不会触发 : pages.page.index

我想要做的是实际上检测那些不会被触发的上述转换observes('currentPath').沿着这些方向,如果我this.get('currentPath');在我的函数内部,我得到的东西,topic.fromParams但我实际上对URL路径感兴趣,例如/t/this-is-my-url-slug.

简单地说,我想检测应用程序的来源:

/t/this-is-my-url-slug
Run Code Online (Sandbox Code Playgroud)

/t/another-url-slug
Run Code Online (Sandbox Code Playgroud)

能够捕获路径:/t/another-url-slug

对不起,我有点像Ember n00b,我唯一的体验就是通过话语.有任何想法吗?

Luk*_*lia 7

您不需要任何特定于Ember的东西来执行此操作.根据您使用的是hash还是pushstate,您可以使用...

$(window).on('hashchange', function(){
  console.log("Hash URL is " + location.hash.substr(1));
  // Do stuff
});
Run Code Online (Sandbox Code Playgroud)

要么

$(window).on('popstate', function(e) {
  console.log("Hash URL is " + window.location.pathname);
  // Do stuff
});
Run Code Online (Sandbox Code Playgroud)