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

发布于 2024-12-04 23:39:30 字数 536 浏览 1 评论 0原文

我想将键值对作为参数传递给 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眉黛浅 2024-12-11 23:39:30

您应该在 Backbone.Router 中重新定义 _extractParameters 函数。然后所有路由器函数都将被调用,第一个参数是 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');

工作演示位于此处

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文