如何将视图附加到视图内的div?

时间:2012-12-07 15:51:19

标签: ember.js

我试图通过id将视图附加到占位符div,与此处的答案一致:How can I dynamically insert a new template into the DOM with Ember?

insertInfoBox:function (context) {
    var infoBoxView = App.LocatorInfoBox.create({
        context:context
    });

    infoBoxView.appendTo('#infoBoxHolder');
},

我收到以下错误:未捕获错误:

断言失败:您无法附加到现有的Ember.View。请考虑使用Ember.ContainerView。

'infoBoxHolder'div嵌套在另一个视图中,但它本身不是视图。如果我将infoBoxView设置为parentView的子节点(如果我将父节点更改为容器视图),我不清楚如何将视图插入子div“infoBoxHolder”而不是直接插入父容器视图的根元素

1 个答案:

答案 0 :(得分:0)

我不确定你为什么要这样做,但你可以使用didInsertElement函数。类似的东西:

App.MyView = Ember.View.extend({
    didInsertElement: function() {
        $('#elementId').append(...);
    }
});

我这样做的方式是通过模板,例如:

App.MyView = Ember.View.extend({
    template: Ember.Handlebars.compile('{{#if someCondition}}{{MyApp.MyOtherView valueBInding="..."}}{{/else}}');
});