ember.js - 新的 ember.js 路由 : how to connect outlets?

标签 ember.js ember-router

我很困惑如何使用新的路由器方法连接 socket 。

index.html:

...
<script type="text/x-handlebars" data-template-name="application">
  <h4>The application handelbar</h4>
  {{! outlet 1}}
  {{outlet}}
</script>

<script type="text/x-handlebars" data-template-name="index">
  <h4>The index handelbar</h4>
  {{! outlet 2 and 3}}
  {{outlet nav}}
  {{outlet main}}
</script>

<script type="text/x-handlebars" data-template-name="main">
  <h4>The main handelbar</h4>
</script>

<script type="text/x-handlebars" data-template-name="nav">
  <h4>The nav handelbar</h4>
</script>
...

app.js:
...
App.Router.map(function(match) {
  this.resource("index", { path: "/" });
  this.route("test");
});

App.IndexController = Ember.Controller.extend({
});

App.IndexView = Ember.View.extend({
  templateName: 'index'
});
...

此代码呈现 outlet-1。

问题:
  • 为什么要渲染 outlet-1? outlet-1 和“index”是如何连接的?
  • 如何将导出 2 和 3 连接到同一个“索引”站点?

  • 谢谢

    最佳答案

    您需要使用 renderTemplate 方法(或 renderTemplates 方法,取决于您的构建)在路由处理程序中指定这些内容。

    你没有看到的是 Ember 已经为你设置了很多默认值。事实上,Ember 设置的默认值允许您省略整个路由处理程序。

    App.Router.map(function(match) {
      this.resource("index", { path: "/" });
      this.route("test");
    });
    App.IndexRoute = Ember.Route.extend({
      renderTemplate: function() {
         this.render(); 
         /* this is the default, it will basically render the
            default template, in this case 'index', into the 
            application template, into the main outlet (i.e. your 
            outlet 1), and set the controller to be IndexController.
         */
    
      }
    });
    

    你想要的是在 renderTemplate 函数中渲染额外的模板,像这样:
      renderTemplate: function() {
         this.render("index"); 
         // this renders the index template into the primary unnamed outlet. 
         this.render("navtemplate", {outlet: "nav"}); 
         // this renders the navtemplate into the outlet named 'nav'.
         this.render("main", {outlet: "main"});
         // this renders the main template into the outlet named 'main'.
      }
    

    希望这可以帮助。

    关于ember.js - 新的 ember.js 路由 : how to connect outlets?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14365749/

    相关文章:

    ember.js - 我什么时候应该在 EmberJS 中使用 Controller 和路由?

    javascript - 在子模板中显示父模板

    javascript - Ember.Js 反射协会

    javascript - Ember Inspector 如何从应用程序获取数据?

    javascript - 运行测试时出现 EmberJS 全局错误

    ember.js - TransitionTo 和新的 Ember Router

    javascript - 如何删除奇怪的 Ember.js View 边距

    ember.js - ember-data-1.0.0 activemodeladapter 错误在传递给 `id` 的哈希中包含 `push`

    ruby-on-rails-3 - Ember 路线和 Rails 路线

    ember.js - 具有动态分段的资源并在嵌套路由中检索模型