问题
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