Backbone.js 路由器中的键值对参数处理 [英] Key value pair params handling in Backbone.js Router

查看:13
本文介绍了Backbone.js 路由器中的键值对参数处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

var MyRouter = Backbone.Router.extend({路线:{仪表板?:参数":show_dashboard"},显示仪表板:功能(参数){控制台日志(参数);}});

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

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

非常感谢,

马诺

解决方案

你应该在 Backbone.Router 中重新定义 _extractParameters 函数.然后所有路由函数将被调用,第一个参数是 params 对象.

//带有自定义参数提取器的骨干路由器var Router = Backbone.Router.extend({路线:{'dashboard/:country/:city/?:params': 'whereAmIActually','dashboard/?:params': 'whereAmI'},whereAmIActually:函数(参数,国家,城市){console.log('whereAmIActually');控制台日志(参数);},whereAmI:函数(参数){console.log('whereAmI');控制台日志(参数);},_extractParameters:函数(路由,片段){var 结果 = route.exec(fragment).slice(1);result.unshift(deparam(result[result.length-1]));返回 result.slice(0,-1);}});//简化的 $.deparam 模拟var deparam = function(paramString){变量结果 = {};如果(!参数字符串){返回结果;}$.each(paramString.split('&'), function(index, value){如果(值){var param = value.split('=');结果[参数[0]] = 参数[1];}});返回结果;};var 路由器 = 新路由器;Backbone.history.start();//此调用假定 url 已更改Backbone.history.loadUrl('dashboard/?planet=earth&system=solar');Backbone.history.loadUrl('dashboard/usa/la/?planet=earth&system=solar');

工作演示在这里.

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

解决方案

You should redefine _extractParameters function in Backbone.Router. Then all router functions will be invoked with the first parameter being params object.

// 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.

这篇关于Backbone.js 路由器中的键值对参数处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆