Javascript 在Ember.js中,如何从App.Router获取rootURL?
如何在控制器中的App.Router中获取rootURL以用于JSON请求 如果我像这样指定一个rootURL:Javascript 在Ember.js中,如何从App.Router获取rootURL?,javascript,ember.js,ember-router,ember-controllers,Javascript,Ember.js,Ember Router,Ember Controllers,如何在控制器中的App.Router中获取rootURL以用于JSON请求 如果我像这样指定一个rootURL: App.Router.reopen({ rootURL: '/site1/' }); FooController = Ember.ObjectController.extend({ needs: ["application"], actions: { examine: function() { var rootURL = this.get
App.Router.reopen({
rootURL: '/site1/'
});
FooController = Ember.ObjectController.extend({
needs: ["application"],
actions: {
examine: function() {
var rootURL = this.get('controllers.application.router.rootURL');
$.getJSON(rootURL + "/examine/" + id).then(function(response) {
// do stuff with response
});
}
}
});
我希望能够做到以下几点:
App.Router.reopen({
rootURL: '/site1/'
});
FooController = Ember.ObjectController.extend({
needs: ["application"],
actions: {
examine: function() {
var rootURL = this.get('controllers.application.router.rootURL');
$.getJSON(rootURL + "/examine/" + id).then(function(response) {
// do stuff with response
});
}
}
});
路由器被注入到所有路由中,您可以将该操作向上移动到该路由,并从中抓取路由器
FooRoute = Ember.Route.extend({
actions: {
examine: function() {
var rootURL = this.get('router.rootURL');
$.getJSON(rootURL + "/examine/" + id).then(function(response) {
// do stuff with response
});
}
}
});
或者,您可以在路由设置控制器时将属性添加到控制器
FooRoute = Ember.Route.extend({
setupController: function(controller,model){
this._super(controller, model);
controller.set('rootURL', this.router.rootURL);
}
});
示例:以下代码在Ember 2.10(关于服务和组件)上对我有效: 我应该补充一点,这通常是一个坏主意,但也许对你有帮助