Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define a base url in Backbone.js router

I want to be able to specify a base url for all my routes for development. Some of the devs have machines setup to work out of subdirectories instead of the application sitting at the webroot. I would like to only define these routes once. Instead of: routes: { "toms-subdirectory/users": "init_users" } I would like:

routes: {
    "/users": "init_users"
}

and be able to specify a baseurl for that installation so I could easily make it work across installs.

Right now I'm forcing them by defining a route object using bracket syntax to keep the dynamic keys and assigning that to the routes property inside the route. I was wondering if there is a cleaner way.

my_routes = {};
my_routes[window.webroot + '/users'] = 'init_users';

MyRoute = Backbone.Router.extend({ routes: my_routes })
like image 667
Evil Buck Avatar asked Feb 20 '12 20:02

Evil Buck


People also ask

How do you define base URL?

The URL found in the address bar of the front page of a website is its base URL. In other words, the common prefix found while navigating inside a given website is known as the base URL. One can select a base URL from the list of those available with help of the URL general properties page.

What is base URL in JavaScript?

To get the base URL in JavaScript, we use the window. location. origin and window. location.

How do I find the URL of a base URL?

There's nothing in the Android URI class that gives you the base URL directly- As gnuf suggests, you'd have to construct it as the protocol + getHost(). The string parsing way might be easier and let you avoid stuffing everything in a try/catch block.

Does anyone use Backbone JS?

Backbone. Backbone has been around for a long time, but it's still under steady and regular development. It's a good choice if you want a flexible JavaScript framework with a simple model for representing data and getting it into views.


1 Answers

You can use Backbone.history.start like this

Backbone.history.start({
    pushState: true,
    root: "/public/search/"
});
like image 153
abraham Avatar answered Oct 06 '22 01:10

abraham