骨干视图没有填充

时间:2013-03-07 03:57:05

标签: javascript backbone.js

我正在将一个集合和一个模型传递给我的观点:

 popModel = new GenreModel({genre_name:'Pop',genre_id:'pop'});
 popView = new GenreView ({model:popModel,collection:new PopCol()}); 

这是View的实现:

Backbone.View.extend ({
    className:'sect',

    initialize: function ()
    {
        context = this;
        this.collection.fetch ({
            success:function ()
            {
                context.render();
            }
        });     
    },

    render: function ()
    {
        console.log("hello");
        this.$el.html (_.template(view,this.model.toJSON()));
        this.collection.each (function (video) 
        {
            genreLi = new GenreLi ({model:video});
            this.$(this.model.genre_id).append (genreLi.el);
        },this);

        return this;
    },

});

我的问题是,当我调用console.log(popView.el)时,我得到以下内容:

<div class="sect"></div>
hello 

视图没有填充,但是当我调用console.log(popView.render.el)时,我得到了:

hello 
<div class=​"sect">​…​</div>​
hello 

View然后正确呈现。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

我猜你试图在ul li列表中显示所有相同类型的视频。 我认为你在获取元素时错过了'#'。

render: function ()
{
    ...
    this.collection.each (function (video) 
    {
        genreLi = new GenreLi ({model:video});
        // if you have already defined elements by genre id and not within the view's el
        // This is not good as a view should attach new html into dom elements under it 
        $('#'+this.model.genre_id).append (genreLi.el);

        // if you have defined elements by genre id within in the view'w el then
        this.$el.find('#'+this.model.genre_id).append (genreLi.el);

    },this);

    return this;
},