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

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-22 06:45:32

问题


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


回答1:


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();
});



回答2:


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




回答3:


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

  }
};


来源:https://stackoverflow.com/questions/20957895/how-to-specify-a-dynamic-root-url-in-ember-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!