我使用建立在EmberJS上的Discourse(http://www.discourse.org/),並隨時嘗試觀察URL的變化,例如,當打開一個新的話題。我見過的答案觀察currentPath,例如這裏: Detect route transitions in EmberJS 1.0.0-pre.4在EmberJS中檢測URL更改和抓取URL(使用Discourse)
App.ApplicationController = Ember.Controller.extend({
routeChanged: function(){
// the currentPath has changed;
}.observes('currentPath');
});
但我試圖檢測任何 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
到
/t/another-url-slug
和能夠捕捉到的路徑:/t/another-url-slug
對不起,但我有點Ember n00b,我唯一的經驗就是通過Discourse。有任何想法嗎?
你有看看這個http://stackoverflow.com/questions/16471068/how-do-i-retrieve-the-path-of-a-route/16471545#16471545? – intuitivepixel
如果你的用例更具通用性,你也可以使用'window.location.href'和'hashchange'事件並在URL字符串上做一個「」.split('#')......只是想着 – intuitivepixel
Didn'看到那個,不。這是一種計算URL的潛在方式,但僅currentPath是不夠的,我還需要有模型。 'topic.fromParams'不能單獨生成路由,除非我知道哪個主題ID,並且我無法弄清楚如何檢測當前params中的哪個主題。這是使用Ember路由,它不使用散列路由,而是正確形成路由,例如'localhost/t/slug',所以散列變化檢測將不起作用。 – kaptron