What controls where Ember's Loading Route is displayed?

天涯浪子 提交于 2019-12-19 00:22:43

问题


I would have thought that the LoadingRoute would display its template in the {{outlet}} of the main AppView, but it doesn't seem like it does. What determines where it goes?

Here's a JS Bin of my problem. The Loading message isn't showing up where I expect.


回答1:


Suppose that you have this mapping:

App.Router.map(function() {
  this.route("foo")
});

When is transitioned to foo route. It template will be inserted in that was specified in the into property of render method. By example:

App.FooRoute = Ember.Route.extend({
    renderTemplate: function() {
      this.render("foo", { into: "sometemplate" })
    }
});

Case this isn't setted, the foo route will retrieve the parent route, in that case ApplicationRoute, and insert the template foo, into application template. This is the default behavior when you don't override the renderTemplate method.

But when no one of that conditions happens, this is the behavior of LoadingRoute, because it doesn't have the ApplicationRoute as parent. Than ember insert the template in the body tag, or more specifically in App.rootElement.




回答2:


Indeed it looks that it is inserted right before the closing tag of the tag with class ember-application. You can control to which outlet it is inserted using renderTemplate:

App.LoadingRoute = Ember.Route.extend({
  renderTemplate: function() {
    this.render('loading', {
      outlet: 'loading',
      into: 'application'
    });
  }
});

Then place the loading outlet wherever you want in the application template:

<script type="text/x-handlebars" data-template-name="application">
  <div id="twenty-fifth-cdu-production">

    {{#view App.Sidebar}}
    <div id="left-panel">
      <ul>
      <li><a href="#one">One</a></li>
      <li><a href="#two">Two</a></li>
      <li><a href="#three">Three</a></li>
      </ul>
    </div>
    {{/view}}

    <div id="center-panel" class="container-fluid">
      {{outlet}}
      {{outlet "loading"}}
    </div>
  </div>
</script>

Note that the name of the default outlet (i.e., {{outlet}}) is main. But trying to use the default outlet for rendering the App.LoadingView creates problems.

Demo: http://jsbin.com/asizim/2/




回答3:


If you increase the timeout you will be able to notice that loading template is attached at the end of document. It is probably designed to be used with overlays of fixed positioned elements.

You can add another outlet (called loading in example below) and force rendering of loading template into it with Route renderTemplate hook:

App.LoadingRoute = Ember.Route.extend({
    renderTemplate: function() {
        this.render("loading", { outlet: 'loading', into: 'application' });
    }
});

Check out this example: http://jsbin.com/ipagut/5#/#two




回答4:


My guess is that this behavior is intentional, so the LoadingRoute could work while the ApplicationRoute itself is loading. Rendering the application template manually should allow you to render into one of its outlets.

App.LoadingRoute = Ember.Route.extend({
    renderTemplate: function() {
        this.render("application");
        this.render("loading", { outlet: "loading", into: "application" });
    }
});


来源:https://stackoverflow.com/questions/17837128/what-controls-where-embers-loading-route-is-displayed

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