在我的backbone.js项目中,我需要一个仅在window.location.hash
更改时触发的方法。我为我的问题找到了一些替代解决方案 here .
此外,我可以通过在主干 View 初始化函数
中创建一个事件来解决这个问题,如下所示
Backbone.View.extend({
initialize() {
$(window).on('hashchange',()=> {
console.log('yes hashchange events works')
});
}
但我寻求一种使用 backbone.js
eventDelegates
或 listenTo
提前致谢
最佳答案
您可以为此范围使用Backbone.history
。
History serves as a global router (per frame) to handle hashchange events or pushState, match the appropriate route, and trigger callbacks. You shouldn't ever have to create one of these yourself since Backbone.history already contains one.
因为它会触发回调,所以您可以监听 hashchange
事件:
Backbone.history.on("all", function (route, router) {
console.log(window.location.hash);
});
或
Backbone.history.on("route", function () {
console.log(window.location.hash);
});
关于javascript - 如何在backbone.js事件委托(delegate)中使用 "hashchange"事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37023773/