1

我将一个集合和一个模型传递给我的视图:

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

这是视图的实现:

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">​…​&lt;/div>​
hello 

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

4

1 回答 1

1

我猜你正试图在 ul li 列表中显示所有相同类型的视频。我认为您在获取元素时缺少 id 之前的“#”。

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;
},
于 2013-03-07T04:20:37.837 回答