使用渲染助手继承奇异控制器

时间:2014-06-17 21:53:18

标签: ember.js

我正在尝试使用每个对象的渲染助手为一组对象(对话)渲染一组选项卡。这不是路由的一部分,因为它是接口的持久部分。我遇到了一个问题,只有与模型同名的视图才能获得预期的控制器(即面板内容而不是标签页)。

我有一个聊天模型,对象控制器和数组控制器(这里有意简化):

App.Chat = DS.Model.extend({ });

App.ChatsController = Ember.ArrayController.extend({
  needs: 'application',
  content: Ember.computed.alias('controllers.application.currentChats'),
});

App.ChatController = Ember.ObjectController.extend({ });

ArrayController需要需要/内容属性,因为聊天内容在应用程序控制器中加载。我使用currentChats名称,因为其他路由可能会加载非当前聊天。

App.ApplicationController = Ember.Controller.extend({
  init: function(){
    this.store.find('chat', {"current": true});
    this.set('currentChats', this.store.all('chat'));
  }
});

使用适当的控制器渲染聊天内容没有任何困难(进入'聊天'模板)。但是,聊天选项卡被赋予默认的ObjectController,因此无法触发操作。

<script type="text/x-handlebars" id="application">
    <!--application template-->
    {{outlet chats}}
</script>

<script type="text/x-handlebars" id="chats">
<div id="chats">
  <ul id="chat-tabs">
    {{#each}}
        {{render 'chatTab' this}}
    {{/each}}
  </ul>
  {{#each}}
    {{render 'chat' this}}
  {{/each}}
</div>
</script>

<script type="text/x-handlebars" id="chatTab">
<!--tab template-->
</script>

<script type="text/x-handlebars" id="chat">
    <!--chat template-->
</script>

应用程序路由器如下:

App.ApplicationRoute = Ember.Route.extend({
    model: function(){ },
    renderTemplate: function(){
        this.render('application', { });
        this.render('chats', {
            into: 'application',
            outlet: 'chats',
            controller: 'chats'
        });
    }
});

这似乎完全取决于模板的命名。名为“chat”的模板继承了正确的控制器,但尽管接收聊天作为模型,chatTab仍然没有。有没有办法强制视图继承正确的控制器?或者我是以特殊的方式解决这个问题。

非常感谢你对这位Ember新手的帮助。

安德鲁

1 个答案:

答案 0 :(得分:0)

它完全取决于提供给渲染的名称。最简单的方法是创建另一个控制器并扩展聊天控制器。

App.ChatTabController = App.ChatController.extend();
相关问题