Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 332
Danny Garcia Avatar asked Jan 06 '14 19:01

Danny Garcia


People also ask

How to log a route change in Ember JS?

The Ember.js uses the HashChange event that helps to know change of route; this can be done by implementing HashLocation object. As an application grows in complexity, the logging route keeps track of the router. The above code translates transition events to the log modifier.

What is a dynamic segment in Ember JS?

The dynamic segment is part of an URL. Wildcard routes used for matching the multiple routes. If you have not define Route, Controller, View, and Template classes objects, then Ember.js will automatically generates these objects into your application. The Ember.js will automatically generate the route if you are not defined in your application.

How do I use map in Ember router?

The map () method of your Ember application's router can be invoked to define URL mappings. When calling map (), you should pass a function that will be invoked with the value this set to an object which you can use to create routes. Now, when the user visits /about, Ember will render the about template.

How to display the retrieved information in a template in Ember JS?

In Ember.js it is necessary to set up the controller that helps the template to display the retrieved information. Ember.js supports two built-in controllers Ember.ObjectController and Ember.ArrayController. These are presents a model's properties to a template, along with any additional display-specific properties.


3 Answers

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();
});
like image 160
jesenko Avatar answered Oct 17 '22 02:10

jesenko


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

like image 43
Jim Barrows Avatar answered Oct 17 '22 01:10

Jim Barrows


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'];

  }
};
like image 28
Law Avatar answered Oct 17 '22 01:10

Law