javascript Backbone.js 路由器中的键值对参数处理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7445353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 00:09:39  来源:igfitidea点击:

Key value pair params handling in Backbone.js Router

javascriptbackbone.jsparamsquery-parameters

提问by Manokaran K

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.

我想将键值对作为参数传递给 Backbone 路由,并希望在调用映射函数之前将其反序列化为 javascript 对象。

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.

当我转到 "http://...#dashboard?key1=val1&key2=val2" 时,应该在控制台上打印 {key1: "val1", key2: "val2"}。

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??

我目前在每个映射函数中使用 jQuery BBQ 的 $.deparam 方法来获取反序列化对象。如果我可以扩展 Router 并只定义一次,这样 params 就可以在所有映射函数中作为对象访问,那就太好了。什么是干净的方法来做到这一点?这有什么陷阱吗??

Much thanks,

非常感谢,

mano

马诺

回答by avrelian

You should redefine _extractParametersfunction in Backbone.Router. Then all router functions will be invoked with the first parameter being paramsobject.

您应该_extractParametersBackbone.Router. 然后所有路由函数将被调用,第一个参数是params对象。

// Backbone Router with a custom parameter extractor
var Router = Backbone.Router.extend({
    routes: {
        'dashboard/:country/:city/?:params': 'whereAmIActually',
        'dashboard/?:params': 'whereAmI'
    },
    whereAmIActually: function(params, country, city){
        console.log('whereAmIActually');
        console.log(arguments);
    },
    whereAmI: function(params){
        console.log('whereAmI');
        console.log(arguments);
    },
    _extractParameters: function(route, fragment) {
        var result = route.exec(fragment).slice(1);
        result.unshift(deparam(result[result.length-1]));
        return result.slice(0,-1);
    }
});

// simplified $.deparam analog
var deparam = function(paramString){
    var result = {};
    if( ! paramString){
        return result;
    }
    $.each(paramString.split('&'), function(index, value){
        if(value){
            var param = value.split('=');
            result[param[0]] = param[1];
        }
    });
    return result;
};

var router = new Router;
Backbone.history.start();

// this call assumes that the url has been changed
Backbone.history.loadUrl('dashboard/?planet=earth&system=solar');
Backbone.history.loadUrl('dashboard/usa/la/?planet=earth&system=solar');

The working demo is here.

工作演示在这里