可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
Say I have a backbone router like:
routes:
"" : "homepage"
"catalog/:id" : "catalogPage"
"catalog/:id/products/:id2" : "productPage"
homepage : -> doStuff()
catalogPage: (id) -> doOtherStuff()
productPage: (id, id2) -> doEvenMoreStuff()
and a function:
executeBefore = -> console.log("hello")
If I want executeBefore to be called and executed each time a route is called and before the corresponding route method, is there a simple way to do it apart from inserting a call to executeBefore at the beginning of every route method ?
回答1:
You can override the route
function in your router class to intercept the route calls :
var Router = Backbone.Router.extend({
routes: {
"" : "homepage",
"catalog/:id" : "catalogPage"
},
route: function(route, name, callback) {
var router = this;
if (!callback) callback = this[name];
var f = function() {
console.log('route before', route);
callback.apply(router, arguments);
console.log('route after', route);
};
return Backbone.Router.prototype.route.call(this, route, name, f);
},
homepage: function() {
console.log("homepage");
},
catalogPage: function(id) {
console.log("catalogPage "+id);
}
});
var r = new Router();
Backbone.history.start();
And a demo http://jsfiddle.net/nikoshr/EdLzh/
回答2:
Since Feb, 13, 2014, you can use router.execute(callback, args, name)
(http://backbonejs.org/#Router-execute) for this.
So your interceptor will be looked something like this
routes: {
'': 'homepage',
'catalog/:id': 'catalogPage',
'catalog/:id/products/:id2': 'productPage'
},
execute: function(callback, args, name) {
// Do your stuff here
executeBefore();
if (callback) callback.apply(this, args);
}
https://github.com/jashkenas/backbone/commit/791033dc1aab53e9e5c9366f64a854224f851231
回答3:
you can also use backbone.routefilter plugin.
you'll be able to set a filter for all routes
var Router = Backbone.Router.extend({
routes: {
"": "index",
"page/:id": "page"
},
before: function( route, params ) { ... },
after: function( route, params ) { ... },
index: function(){ ... },
page: function( route ){ ... }
});
Or select some routes
var Router = Backbone.Router.extend({
routes: {
"": "index",
"page/:id": "page"
},
before: {
"": function( route ) { ... },
"page/:id": function( route ) { ... }
},
after: function( route ) { ... },
index: function(){ ... },
page: function( route ){ ... }
});
回答4:
You can play with the Events :
Backbone.history.on('route', function () {
// Do your stuff here.
executeBefore();
});
visiting #help/uploading will fire a route:help event from the router.
Apparently, this would be triggered before the redirection :
https://github.com/documentcloud/backbone/blob/master/backbone.js#L1243
Backbone.history.route(route, function(fragment) {
var args = router._extractParameters(route, fragment);
callback && callback.apply(router, args);
router.trigger.apply(router, ['route:' + name].concat(args));
router.trigger('route', name, args);
Backbone.history.trigger('route', router, name, args);
});