在 javascript/backbone 中前置/附加哈希键

发布于 2024-12-15 12:39:44 字数 2389 浏览 1 评论 0原文

我有一个轻微的困境。 这对于backbone.js来说并不常见,但它肯定会导致我在使Backbone.Router.routes工作时出现问题:

问题: 我的 js 中有一堆硬编码路由,它们遵循键值对哈希模式,如下所示:

whatever.route : {"/url/goes/here":"functionNameHere"}

基本上,它的作用是将 url 绑定到函数名称,以便在 URL 更改时调用。

我遇到的问题是我需要在 url 前面加上 lang/locale 字符串,以便该字符串看起来像“/en/url/goes/here”

// this will always return "en" or "fr" or aany 2 letter language code    
var lang = window.location.pathname.split("/")[1].length ==2?window.location.pathname.split("/")[1]:false;


workspace = new Workspace( //Workspace is basically just a Backbone.router.extend Object
            {
                // the routes obj is basically a sequence of routes
                routes: {
                    "/":                    "home",
                    "/home":                "home",
                    "/terms":               "terms",
                    "/news":                "blog",
                    "/news/:title":         "blogpost",
                    "/about":               "about",
                    "/about/partners":      "partners",
                    "/about/people":        "people",
                    "/values/ethics":       "ethics",
                    "/values/innovation":   "innovation",
                    "/work":                "work",
                    "/work/process":        "process",
                    "/work/awards":         "awards",
                    "/work/:id":            "workdetail",
                    "/contact":             "contact",
                    "/contact/join":        "joinus",
                    "/contact/enquiries":   "enquiries"
                },
                lang : lang
            }
        );

我最初的想法是:

....routes{ lang+"/url/goes/here": "functionNameHere",
...

没有骰子错误 接下来我尝试使用:

....routes{ eval(lang+"/url/goes/here"): "functionNameHere", ...

没有骰子了..

肯定有一种方法可以动态动态地预先添加哈希键?

有人吗?

非常感谢

解决方案感谢 TJ(见下文)

如果有人对 Backbone.js 特定方式感兴趣。 我所做的就是在我的初始化函数上使用下面 TJ 建议的解决方案,如下所示:

Nice TJCrowder!

不确定我是否应该编辑原稿

initialize:  function(params){
        var tmpr = {};
        for(var i in params.routes)
        {
            tmpr[params.lang+i] = params.routes[i];
        }
        this.routes = tmpr;
......

I have a slight dilema.
This isn't general to backbone.js but it's certainly causing me issues getting the Backbone.Router.routes working:

The problem:
I have a bunch of hard coded routes in my js these follow a key value pair hash pattern like:

whatever.route : {"/url/goes/here":"functionNameHere"}

basically what this does is to bind a url to a function name to call when the URL is changed.

The problem I have is that I need to prepend the url with a lang/locale string so that the string would look like "/en/url/goes/here"

// this will always return "en" or "fr" or aany 2 letter language code    
var lang = window.location.pathname.split("/")[1].length ==2?window.location.pathname.split("/")[1]:false;


workspace = new Workspace( //Workspace is basically just a Backbone.router.extend Object
            {
                // the routes obj is basically a sequence of routes
                routes: {
                    "/":                    "home",
                    "/home":                "home",
                    "/terms":               "terms",
                    "/news":                "blog",
                    "/news/:title":         "blogpost",
                    "/about":               "about",
                    "/about/partners":      "partners",
                    "/about/people":        "people",
                    "/values/ethics":       "ethics",
                    "/values/innovation":   "innovation",
                    "/work":                "work",
                    "/work/process":        "process",
                    "/work/awards":         "awards",
                    "/work/:id":            "workdetail",
                    "/contact":             "contact",
                    "/contact/join":        "joinus",
                    "/contact/enquiries":   "enquiries"
                },
                lang : lang
            }
        );

my intial thought was along the lines of:

....routes{ lang+"/url/goes/here": "functionNameHere",
...

no dice errors
next i try using :

....routes{ eval(lang+"/url/goes/here"): "functionNameHere",
...

no dice again..

surely theres a way to dynamically prepend the hash key on the fly?

Anyone?

Thanks very much

Solution Thanks to T.J. (see below)

If anyone is interested in this in a Backbone.js specific way.
What I did was to use the solution T.J. suggested below on my initialize function like so:

Nice T.J.Crowder!!

not sure if I should edit the orig

initialize:  function(params){
        var tmpr = {};
        for(var i in params.routes)
        {
            tmpr[params.lang+i] = params.routes[i];
        }
        this.routes = tmpr;
......

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

自控 2024-12-22 12:39:44

对象文字中 : 左侧的部分必须是文字(标记[例如,不带引号]或字符串文字[例如,带引号] )。它不能是一个表达式。所以你不能在对象文字中执行此操作。相反,您必须这样做:

var routes = {};
routes[lang + "/url/goes/here"] = "functionNameHere";
// ...
workspace = new Workspace(
            {
                routes: routes,
                lang : lang
            }
        );

在这里,我们使用括号符号将属性分配给 routes 对象。使用括号表示法时,可以使用表达式来确定属性名称。这是 JavaScript 中比较酷但不太为人所知的功能之一,可以使用点分符号和文字属性名称 (obj.foo) 或括号符号和字符串属性名称来访问属性(obj["foo"])。 (事实上​​,这就是您在索引数组时所做的事情——a[0] = 5;——因为数组根本不是真正的数组。)

The part to the left of the : in an object literal must be a literal (either a token [e.g., without quotes] or a string literal [e.g., with quotes]). It cannot be an expression. So you can't do this in an object literal. Instead, you'll have to do this:

var routes = {};
routes[lang + "/url/goes/here"] = "functionNameHere";
// ...
workspace = new Workspace(
            {
                routes: routes,
                lang : lang
            }
        );

There, we're using the bracketed notation to assign properties to the routes object. When you use bracketed notation, you can use an expression to determine the property name. It's one of the cooler, and less-well-known, features of JavaScript that properties can be accessed either with dotted notation and a literal property name (obj.foo) or bracketed notation and a string property name (obj["foo"]). (In fact, that's what you're doing when you index into arrays — a[0] = 5; — because arrays aren't really arrays at all.)

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