我正在使用基于 EmberJS 构建的 Discourse ( http://www.discourse.org/ ),并尝试观察 URL 的任何变化,例如当打开一个新主题时。我已经看到了观察 currentPath 的答案,例如这里: Detect route transitions in EmberJS 1.0.0-pre.4
App.ApplicationController = Ember.Controller.extend({
routeChanged: function(){
// the currentPath has changed;
}.observes('currentPath');
});
但我正在尝试检测任何 URL 更改,而不仅仅是路径更改。正如该答案的评论中提到的:
This observer doesn't fire when transitioning from for example
/pages/1
to/pages/2
because the path is staying the same:pages.page.index
我想做的是实际上检测那些不会被 observes('currentPath')
触发的上述转换。沿着这些思路,如果我在函数内部执行 this.get('currentPath');
,我会得到类似 topic.fromParams
的内容,但我实际上对 URL 感兴趣路径例如/t/this-is-my-url-slug
。
简单来说,我想检测应用程序的运行时间:
/t/this-is-my-url-slug
至
/t/another-url-slug
并且能够捕获路径:/t/another-url-slug
抱歉,我有点像 Ember n00b,我唯一的经验是通过 Discourse。有什么想法吗?
最佳答案
您不需要任何特定于 Ember 的东西来执行此操作。根据您使用的是哈希还是推送状态,您可以使用...
$(window).on('hashchange', function(){
console.log("Hash URL is " + location.hash.substr(1));
// Do stuff
});
或
$(window).on('popstate', function(e) {
console.log("Hash URL is " + window.location.pathname);
// Do stuff
});
关于ember.js - 在 EmberJS 中检测 URL 更改并获取 URL(使用 Discourse),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16560738/