javascript - 主干 View 未填充

标签 javascript backbone.js

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

 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 

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

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

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

最佳答案

我猜您正在尝试在 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;
},

关于javascript - 主干 View 未填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15262787/

相关文章:

javascript - 来自设备的不正确的 JSON 格式

javascript - AngularJS 指令 - 来自 $scope 的模板和其他指令

javascript - jQuery 从 backbone 的脚本标签中选择标记

angularjs - polymer |如何在铁组件页面上加载多个源?

backbone.js - 具有动态 View 的主干

javascript - Angular 5(Angular 2+),第二个守卫不等待第一个守卫完成http请求

javascript - 动态显示保管箱 - html 中的 Javascript/Jquery

javascript - 放置 JQuery 的位置(设置 :visibility via CSS) in a BackBone View

javascript - WebPack sourcemaps 令人困惑(重复文件)

php - 使用 MySQL 和 Backbone JS 检索数据