Danny Garcia
Danny Garcia

Reputation: 841

How to specify a dynamic root URL in Ember.js?

Ember allows for a root URL to be specified on the router here: http://emberjs.com/guides/routing/#toc_specifying-a-root-url

App.Router.reopen({
  rootURL: '/blog/'
});

Is there a way to specify a dynamic URL like: /:region/:locale/?

The rootURL assignment seems to only accept a literal string.

Assets (including Ember) are being loaded from a common directory like /assets/.

Upvotes: 5

Views: 1680

Answers (3)

Law
Law

Reputation: 141

I was able to accomplish this within an instance-initializer - I set the root url as a meta environment variable using ember-cli-meta-options, then applied it to the router

export default {
  name: "router",

  initialize: function( instance ) {

    var router = instance.container.lookup('router:main');
    var options = instance.container.lookup('session:env');
    router.rootURL = options['root'];

  }
};

Upvotes: 1

jesenko
jesenko

Reputation: 1293

You can set rootURL dynamically within Router.init method, e.g.

App.Router.reopen({
  init: function() {
     // set rootURL using regex to extract appropriate
     // rootURL based on current window location
     this.set('rootURL', 
       window.location.pathname.match('/[^/\]*/[^/\]*/')[0]);
     this._super();
});

Upvotes: 7

Jim Barrows
Jim Barrows

Reputation: 3634

You'll have to declare you're root URL '/', and then create the rest as routes/resources under that.

Upvotes: 1

Related Questions