在多个视图实例上调用渲染的最佳方法是什么?

时间:2014-06-14 21:56:40

标签: backbone.js

我尝试在视图的多个实例上调用render。然而,他们有点需要按顺序发生(不是真的)。这是我的观察代码,希望能够自行解释。

var SiteView = Backbone.View.extend({
    el: '#contentArea',
    render: function(siteId) {
        console.log("SiteView.render running",arguments);
        var that = this;
        var siteCollection = new SiteCollection();
        this.collection = siteCollection;
        this.collection.fetch({
            data: {
                returnType: "VERTEX",
                startId: siteId,
            },
            success: function(model, response, options) {
                console.log("SiteView.render.success",arguments);
                var templateHtml = _.template(siteHtml, response);
                that.$el.empty();
                that.$el.html(templateHtml);
                that.renderServersList(response.ID);
                that.renderContainersList(response.ID);
            }
        });
    },
    renderServersList: function(siteId) {
        console.log("SiteView.getServersList running", arguments);
        var that = this; // Removed this line which seemed to be causing the issue :-/
        // This is a view in a separate file provided by require.js
        var serverListView = new VertexListView({el: "#siteServersListBody"});
        serverListView.render("SERVERS", siteId);
    },
    renderContainersList: function(siteId) {
        console.log("SiteView.getContainersList running", arguments);
        // This is a view in a separate file provided by require.js
        var contListView = new VertexListView({el: "#siteContainersListBody"});
        contListView.render("CONTAINERS", siteId);
    }
});

getContainersList和getServersList都正常工作,因为它正在获取正确的数据。但是,当服务器列表呈现时,它还包含整个容器列表,然后容器列表仅包含它应包含的内容。

我之所以将这些请求分开而不是在原始请求中包含容器和服务器,主要是因为容器的大小。它可能相当大。

0 个答案:

没有答案
相关问题