Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在Ember.js中,如何从App.Router获取rootURL?_Javascript_Ember.js_Ember Router_Ember Controllers - Fatal编程技术网

Javascript 在Ember.js中,如何从App.Router获取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中获取rootURL以用于JSON请求

如果我像这样指定一个rootURL:

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(关于服务和组件)上对我有效:

我应该补充一点,这通常是一个坏主意,但也许对你有帮助