使用setElement时未添加Backbone View根元素

时间:2013-12-07 15:22:33

标签: jquery backbone.js

我正在使用setElement将子视图添加到现有视图中。我遇到的问题是没有添加子视图(tagName)的rootElement,但是它的子元素被追加。

主要观点:

  backbone.View.extend ({
    el:'.right-sec',
    initialize: function ()
    {
        this.subView = new SubView ();
        this.render ();
    },
    render: function ()
    {
        this.$el.html (view);
        this.assign (this.subView,".subview");
    },
    assign: function (view,selector)
    {
       view.setElement(this.$(selector)).render();
    },
});

子视图:

backbone.View.extend ({
    className:'sub-view',
    tagName:'div',
    initialize: function ()
    {
        this.collection = new ViewCol ();
        this.collection.on ("reset",this.render,this);
        this.collection.fetch ({reset:true});
    },
    render: function ()
    {
        this.collection.each (this.addView,this);
        return this;
    },
    addView: function (video,index)
    {
        this.$el.append (new View({model:video}).el);
    },
});

查看:

 backbone.View.extend ({
    className:"pull-left",
    initialize: function ()
    {
        this.render ();
    },
    render: function ()
    {
        this.$el.html (_.template(ViewItem,this.model.toJSON()));
        return this;
    },

这是html结果:'

 <div class="right-sec">
      <div class="pull-left"><h2>Apple</h2></div>
      <div class="pull-left"><h2>Pineapple</h2></div>
      <div class="pull-left"><h2>Banana</h2></div>
 </div>

这就是我所期待的:

 <div class="right-sec">
    <div class="sub-view">
      <div class="pull-left"><h2>Apple</h2></div>
      <div class="pull-left"><h2>Pineapple</h2></div>
      <div class="pull-left"><h2>Banana</h2></div>
    </div>
 </div>

导致视图呈现的原因是什么,修复它的最佳方法是什么。

0 个答案:

没有答案