Backbone.js 路由器中的键值对参数处理
我想将键值对作为参数传递给 Backbone 路由,并希望在调用映射函数之前将其反序列化为 JavaScript 对象。
var MyRouter = Backbone.Router.extend({
routes: {
"dashboard?:params" : "show_dashboard"
},
show_dashboard: function(params){
console.log(params);
}
});
当我转到“http://...#dashboard?key1=val1&key2=val2”时,应在控制台上打印 {key1: "val1", key2: "val2"} 。
目前,我在每个映射函数中使用 jQuery BBQ 的 $.deparam 方法来获取反序列化对象。如果我可以扩展 Router 并仅定义一次,这样 params 就可以在所有映射函数中作为对象进行访问,那就太好了。什么是一个干净的方法来做到这一点?这其中是否存在一些陷阱?
非常感谢,
马诺
I want to pass key value pairs as params to Backbone routes and want it to be deserialized to a javascript object before the mapped function is called.
var MyRouter = Backbone.Router.extend({
routes: {
"dashboard?:params" : "show_dashboard"
},
show_dashboard: function(params){
console.log(params);
}
});
When I go to "http://...#dashboard?key1=val1&key2=val2", then {key1: "val1", key2: "val2"} should be printed on the console.
Am currently using jQuery BBQ's $.deparam method inside each mapped function to get at the deserialized object. It would be nice if I can extend Router and define it just once so params is accessible inside all mapped functions as an object. What would be a clean way to do this? And are there some pitfalls in this??
Much thanks,
mano
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该在
Backbone.Router
中重新定义_extractParameters
函数。然后所有路由器函数都将被调用,第一个参数是 params 对象。工作演示位于此处。
You should redefine
_extractParameters
function inBackbone.Router
. Then all router functions will be invoked with the first parameter beingparams
object.The working demo is here.