骨架.js-渲染视图

| 我的ListClass看起来像这样:
var ListView = Backbone.View.extend({

    initialize: function() {
        this.render();
    },  

    events: {
        \'click ul#perpage span\': \'setperpage\'
    },

    setperpage: function(event) {
        this.collection.perpageurl = \'/perpage/\' + $(event.target).text();
        this.collection.fetch();
        this.render();
    },

    render: function() {

    template = _.template(\'\\
    <table>\\
    <% _(collection).each(function(model){%>\\
    <tr><td><%=model.id%></td><td><%=model.name%></td><td><%=model.email%></td></tr>\\
    <%}); %>\\
    </table>\\
    <ul id=\"perpage\">\\
    <li><span>5</span></li>\\
    <li><span>10</span></li>\\
    </ul>\\
    \');

        var context = {collection: this.collection.toJSON()};
        $(this.el).html(template(context));
        $(\'#app\').html(this.el);
        return this;
    }
});
由于某种原因,当我第一次访问加载该视图的页面时,会显示5个项目(应该发生)。但是,当我通过单击ѭ1来触发\“ setperpage \”事件时,即使更新了URL然后调用了
fetch()
,该列表也从未更新。 (我已经看过了带有萤火虫的请求,所以我知道我将在json中获取10个项目)。他们为什么不重新渲染?我仍然只看到5个项目。     
已邀请:
ѭ3是异步的,因此渲染调用仍将使用旧数据。 您需要等待fetch()完成后才能调用render函数。 在您的initialize函数中,将渲染函数绑定到\“ reset \”事件。
this.collection.on(\"reset\", function() { this.render(); }, this);
当您需要新数据时,请致电
this.collection.fetch({reset: true});
    

要回复问题请先登录注册