【问题标题】:Should I use connectOutlet in this scenario in Ember?我应该在 Ember 的这种情况下使用 connectOutlet 吗?
【发布时间】:2013-12-28 04:08:18
【问题描述】:

我有一个 TasksList 应用程序。每个任务中都有 cmets。我正在考虑添加 cmets 作为即插即用的插座。

我有一个显示单个任务的 ViewerRoute:

App.ViewerRoute = Ember.Route.extend({
    activate: function () {
        $(document).attr('title', 'Task View');
    },
   renderTemplate: function () {
        this.render('Comments', { into: "Viewer", outlet: "comment", controller: "Comment" });
    }
});

我的查看器模板有以下 Outlet {{outlet comment}}

我还创建了一个带有一些示例标记的 Comments.hbs 文件:

<div class="row-fluid">
    <div class="well span12">
        <div class="page-header">
          <h3>
            Followups
          </h3>
        </div>
</div>

但是当我运行该页面时,我收到一条错误消息,上面写着“无法调用未定义的方法 connectOutlet”。我将问题三角化到 ember 中的以下函数

_lookupActiveView: function(templateName) {
    var active = this._activeViews[templateName]; //templateName is "Comment" 
    return active && active[0];
  },

问题是这个函数总是返回未定义的。 最终当代码运行到parentView.connectOutlet(options.outlet, view); 它遇到了错误。

我错过了什么吗?

这是我的路由器

App.Router.map(function () {

    this.resource("taskspanel", function () {
        this.resource("viewer", { path: '/viewer/:taskId' }, function () {
        });
        this.resource("new", { path: '/new' });
    });

【问题讨论】:

    标签: javascript jquery ember.js ember-router


    【解决方案1】:

    如果您的命名插座在查看器中,您应该在其中进行渲染,此外,由于您覆盖了查看器的 renderTemplate 钩子并呈现不同的内容,因此看起来不会呈现查看器。

     this.render();
     this.render('Comments', { into: "viewer", outlet: "comment", controller: "Comment" });
    

    http://emberjs.com/guides/routing/rendering-a-template/

    App.PostRoute = App.Route.extend({
      renderTemplate: function() {
        this.render('favoritePost', {   // the template to render
          into: 'posts',                // the route to render into
          outlet: 'posts',              // the name of the outlet in the route's template
          controller: 'blogPost'        // the controller to use for the template
        });
        this.render('comments', {
          into: 'favoritePost',
          outlet: 'comment',
          controller: 'blogPost'
        });
      }
    });
    

    【讨论】:

    • 感谢 kingpin2k,我做到了,但错误仍然存​​在。 parentView 未定义。
    • 查看器在页面中不存在,因此插座将不存在(请参阅更新的答案)
    猜你喜欢
    • 2011-01-17
    • 2022-07-22
    • 2013-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-15
    • 2010-09-05
    • 2017-05-09
    相关资源
    最近更新 更多