我有一个有多个路由的路由器。目前,我在方法本身中转义了单独传递给该方法的每个参数。
出于安全目的,我需要转义 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
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); } });
关于javascript - 是否可以一次转义多个路由中的所有参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56373805/