【问题标题】:Ember.js render into view's outletEmber.js 渲染到视图的出口
【发布时间】:2013-08-06 18:16:47
【问题描述】:

我有一个内部有一个命名出口的视图。此视图与控制器无关,不能用于我的用例。我似乎无法渲染到那个命名的插座。我该如何正确定位它?

如果视图命名为App.UnassociatedView,命名出口为{{outlet potatoOutlet}},我希望关联的控制器和视图分别为App.PotatoControllerApp.PotatoView,我该如何实现?

我试过了

App.ApplicationRoute = Ember.Route.extend({
    renderTemplate: function() {
        this.render();
        this.render('potato', {outlet: 'potatoOutlet', controller: this.controllerFor('potato')});
    }
});

这个

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

还有这个

App.ApplicationRoute = Ember.Route.extend({
    renderTemplate: function() {
        this.render();
        this.render('potato', {outlet: 'potatoOutlet', controller: this.controllerFor('potato'), into: 'unassociated'});
    }
});

这是一个 js 小提琴:http://jsfiddle.net/wmarbut/X8Mu4/

为清晰起见进行编辑

我在小提琴的评论文本中有这个,但忘了把它放在这里。将出口移动到根级别对我来说不是一个选项 b/c 这样做的目标是让UnassociatedView 在页面加载时实际生成出口。我的代码使插座真正起作用,我只是无法连接它们。

【问题讨论】:

    标签: javascript ember.js ember-router


    【解决方案1】:

    我已经稍微修复了你的 jsfiddle。

    App.IndexRoute = Ember.Route.extend({
      renderTemplate: function(){
        this._super();
        var controller = this.controllerFor('potato');
        this.render('potato', {outlet: 'potatoOutlet', controller: controller});
      }
    })
    

    http://jsfiddle.net/X8Mu4/5/

    • 将出口移至根级别
    • 将 renderTemplate 覆盖移至 IndexRoute(因为这是路由到的)
    • 启用 LOG_TRANSITIONS 以帮助调试路由

    我建议不要将 ApplicationView 的模板覆盖为索引。导致混乱。

    您也不必显式创建 ApplicationView。见:Do I have to explicitly create an `ApplicationView` and an `ApplicationController` for every Ember.js application?

    【讨论】:

    • 啊,所以这里的问题是实际中的未关联视图实际上在加载时动态创建了 N 个视图,因此出口不能位于根级别并且必须驻留在该视图中。感谢您的帮助和时间!
    猜你喜欢
    • 2013-04-05
    • 2013-11-27
    • 2013-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-10
    • 2014-03-08
    • 2012-03-04
    相关资源
    最近更新 更多