gpt4 book ai didi

javascript - 是否可以一次转义多个路由中的所有参数?

转载 作者:行者123 更新时间:2023-12-02 23:33:51 25 4
gpt4 key购买 nike

我有一个有多个路由的路由器。目前,我在方法本身中转义了单独传递给该方法的每个参数。

出于安全目的,我需要转义 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com