简体   繁体   English

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

[英]How do I track router change events in Backbone.js

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. 每次应用程序在Backbone.js中切换URL时我都需要运行一个函数,我需要知道URL已更改为的hashtag。 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. 具体来说,我想将新URL发送到分析应用程序。

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. 我认为你最好使用这种方法。

@qwertymk was half way there. @qwertymk就在那里。 You can look for the hashchange event on the window object: 您可以在窗口对象上查找hashchange事件:

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

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

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: 它更精确,因为'all'是一个全部:从Backbone文档引用:

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). 除了事件名称之外,回调被传递与trigger相同的参数(除非你正在监听"all" ,这将导致你的回调接收事件的真实名称作为第一个参数)。

By the way, Backbone best practice is to use listenTo method, eg: 顺便说一下,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. 这样您就不需要手动清理事件侦听器 - 而是将其逻辑绑定到view / model / etc. that uses it. 使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM