Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6798885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:47:00+00:00 2026-05-26T18:47:00+00:00

I have a slight dilema. This isn’t general to backbone.js but it’s certainly causing

  • 0

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;
......
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:47:00+00:00Added an answer on May 26, 2026 at 6:47 pm

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a common question but I have a slight twist to it so
I've wrote this simple piece of code. And I have a slight problem with
Different databases have slight variations in their implementations of SQL. Does PDO handle this?
I have a slight issue with getting Auto Complete to search when the text
I have 3 different transaction tables, which look very similar, but have slight differences.
I have a slight javascript problem, and this is the reason to why i
I have a slight problem with getting the options within the select tag. Some
I have a slight problem with this code: string[] sWords = {Word 1, Word2}
I'm creating a trigger in MySQL but have a slight issue. Here's the trigger
Have a slight problem. Trying to post XML to a server. To do this,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.