是否可以一次转义多个路由中的所有参数?
Is it possible to escape all the parameters in multiple routes at once?
我有一个有多条路由的路由器。目前,我正在转义在方法本身中分别传递给方法的每个参数。
为了安全起见,我需要转义 URL 中传递的所有参数。
class MyRouter extends Backbone.Router
routes:
"student/:id/:name" : "student"
"teacher/:tid/:tname" : "teacher"
"teacher/:tid/:tname/share" : "teacher_share"
student: (id, name) ->
id = _.escape(id)
name = _.escape(name)
#do stuff
teacher: (tid, tname) ->
tid = _.escape(tid)
tname = _.escape(tname)
#do stuff
teacher_share: (tid, tname) ->
tid = _.escape(tid)
tname = _.escape(tname)
#do stuff
是否可以一次转义所有路由中的所有参数,这样我就不必在每个相应的方法中显式转义它们?
您可以覆盖 execute
execute router.execute(callback, args, name)
This method is called internally within the router, whenever a route matches and its corresponding callback is about to be executed. Return false from execute to cancel the current transition. Override it to perform custom parsing or wrapping of your routes, for example, to parse query strings before handing them to your route callback, like so:
var Router = Backbone.Router.extend({
execute: function(callback, args, name) {
if (!loggedIn) {
goToLogin();
return false;
}
args.push(parseQueryString(args.pop()));
if (callback) callback.apply(this, args);
}
});
我有一个有多条路由的路由器。目前,我正在转义在方法本身中分别传递给方法的每个参数。
为了安全起见,我需要转义 URL 中传递的所有参数。
class MyRouter extends Backbone.Router
routes:
"student/:id/:name" : "student"
"teacher/:tid/:tname" : "teacher"
"teacher/:tid/:tname/share" : "teacher_share"
student: (id, name) ->
id = _.escape(id)
name = _.escape(name)
#do stuff
teacher: (tid, tname) ->
tid = _.escape(tid)
tname = _.escape(tname)
#do stuff
teacher_share: (tid, tname) ->
tid = _.escape(tid)
tname = _.escape(tname)
#do stuff
是否可以一次转义所有路由中的所有参数,这样我就不必在每个相应的方法中显式转义它们?
您可以覆盖 execute
execute
router.execute(callback, args, name)
This method is called internally within the router, whenever a route matches and its corresponding callback is about to be executed. Return false from execute to cancel the current transition. Override it to perform custom parsing or wrapping of your routes, for example, to parse query strings before handing them to your route callback, like so:var Router = Backbone.Router.extend({ execute: function(callback, args, name) { if (!loggedIn) { goToLogin(); return false; } args.push(parseQueryString(args.pop())); if (callback) callback.apply(this, args); } });