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

发布于 2024-12-26 22:47:46 字数 138 浏览 0 评论 0原文

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

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

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

挖个坑埋了你 2025-01-02 22:47:47

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

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

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

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

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

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

濫情▎り 2025-01-02 22:47:47

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

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

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

// jQuery example
$(window).bind('hashchange', function(){
    console.log(window.location.hash);
});
多孤肩上扛 2025-01-02 22:47:47

将其放在代码的顶部,

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;
});

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

稍后您可以使用

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

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

Later you can use

Backbone.history.bind('before:url-change', function(path,e){
    console.log("before url change", path, e)
});
酷到爆炸 2025-01-02 22:47:47

还有另一个“Backbone.history.on('route', ...)”事件,也可以工作,您可以发现它在库中被触发):

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

它更精确,因为“all”是包罗万象的:
引用 Backbone 文档:

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

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

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

这样您就不需要手动清理事件侦听器 - 相反,它在逻辑上绑定到视图/模型/等。使用它的。

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', function() { debugger; });

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

Trigger one or many events, firing all bound callbacks. Callbacks are passed the same arguments as trigger is, 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).

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文