javascript 如何在 Backbone.js 中跟踪路由器更改事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8841418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 04:45:12  来源:igfitidea点击:

How do I track router change events in Backbone.js

javascriptbackbone.js

提问by Thomas Hunter II

I need to run a function every time the application switches URLs in Backbone.js, and I need to know the hashtag the URL has changed to. I'm assuming there is an event that I can bind to but I haven't been able to figure out which event and what object to bind to.

每次应用程序在 Backbone.js 中切换 URL 时,我都需要运行一个函数,并且我需要知道 URL 已更改为的主题标签。我假设有一个我可以绑定到的事件,但我一直无法弄清楚要绑定到哪个事件和哪个对象。

Specifically I want to ship the new URL to an analytics application.

具体来说,我想将新 URL 发送到分析应用程序。

采纳答案by Thomas Hunter II

@qwertymk was half way there. You can look for the hashchange event on the windowobject:

@qwertymk 已经到了一半。您可以在window对象上查找 hashchange 事件:

// jQuery example
$(window).bind('hashchange', function(){
    console.log(window.location.hash);
});

回答by Theus G

I know this is an old post, but like @kirk suggested, Backbone.js already has it built it.

我知道这是一篇旧帖子,但就像@kirk 建议的那样,Backbone.js 已经构建了它。

Backbone.history.on("all", function (route, router) {
    //console.log(window.location.hash);
});

I think you'd be better off using this method.

我认为你最好使用这种方法。

回答by mdikici

put this somewhere top in your code

把它放在你代码的顶部

Backbone.History.prototype.navigate = _.wrap(Backbone.History.prototype.navigate, function(){
    // Get arguments as an array
    var args = _.toArray(arguments);
    // firs argument is the original function
    var original = args.shift();
    // Set the before event
    Backbone.history.trigger('before:url-change', args);
    // Call original function
    var res = original.apply(this, args);
    // After event
    Backbone.history.trigger('url-changed');
    // Return original result
    return res;
});

the code above will wrap History.navigate function and will trigger "before:url-change" and "url-changed" when it is called

上面的代码将包装 History.navigate 函数,并在调用时触发“before:url-change”和“url-changed”

Later you can use

以后你可以用

Backbone.history.bind('before:url-change', function(path,e){
    console.log("before url change", path, e)
});

回答by Ashot

There is another, "Backbone.history.on('route', ...)" event, that works too and you can find it being triggered in the library):

还有另一个“Backbone.history.on('route', ...)”事件,它也有效,你可以在库中找到它被触发):

Backbone.history.on('route', function() { debugger; });

Backbone.history.on('route', function() { debugger; });

It is more precise, because 'all' is a catch all: Quote from Backbone documentation:

它更精确,因为“全部”是一个包罗万象的内容:引自 Backbone 文档:

Trigger one or many events, firing all bound callbacks. Callbacks are passed the same arguments as triggeris, apart from the event name (unless you're listening on "all", which will cause your callback to receive the true name of the event as the first argument).

触发一个或多个事件,触发所有绑定的回调。trigger除了事件名称之外,回调传递的参数与原样相同(除非您正在侦听"all",这将导致您的回调接收事件的真实名称作为第一个参数)。

By the way, Backbone best practice is to use listenTo method, e.g.:

顺便说一句,Backbone 最佳实践是使用 listenTo 方法,例如:

myView.listenTo(Backbone.history, 'route', function() { debugger; })

myView.listenTo(Backbone.history, 'route', function() { debugger; })

This way you don't need to clean up the event listener manually - instead it is logically bound to the view/model/etc. that uses it.

这样你就不需要手动清理事件监听器——而是在逻辑上绑定到视图/模型/等。使用它。