Backgrid:渲染不被调用

时间:2016-02-29 10:34:24

标签: backbone.js underscore.js backgrid backbone-layout-manager

Backgrid正在渲染

<table class="backgrid"></table>

但没有别的。 Backgrid中的断点:未达到render()。我是Backbone新手改编其他人的代码所以我不确定应该发生什么但是LayoutManager:render()被调用..它似乎永远不会到达Backgrid ......我想要的数据显示正在被取出并且看起来好像它们的格式正确...但是必须承认,一旦它们被包裹在Backbone集合中就很难分辨。任何关于如何调试/为什么Backgrid渲染的指针都不会被感激地接收。

以下代码:

ListenView.js

define([
    'backbone',
        'underscore',
        'backgrid',
    'app/models/PersonModel',
    'app/collections/PersonCollection',
    'app/views/PersonListView',
    'hbs!app/templates/listen_template'
    ],
    function(
    Backbone,
        _,
        Backgrid,
        Person,
    PersonCollection,
    PersonListView,
    listenTemplate
    ) {

    App = window.App || {};
    var ListenView = Backbone.View.extend({
        template: listenTemplate,
        initialize: function(params) {
            //fetch the list of seen people
            this.model.attributes.seenPeople.fetch( {
                success: function(coll, resp) {
                    //console.log(coll);
                }
            });
        },
        afterRender: function() {
            //initialise person view
        console.log("creating Backgrid");
        this.seenPeopleView = new Backgrid.Grid({
                        columns: [{
                                name: "email",
                                label: "Email",
                                cell: "string"  
                        },{
                                name: "id",
                                label: "ID",
                                cell: "integer"  
                        }, {
                                name: "title",
                                label: "Title",
                                cell: "string" }
                        ],
                        collection: this.model.attributes.seenPeople
                    });
            this.seenPeopleView.render();
                $('#seen-people-list').append(this.seenPeopleView.el);
    }

2 个答案:

答案 0 :(得分:0)

关于fetch的成功方法,你应该调用afterRender。

 var self=this;
 this.model.attributes.seenPeople.fetch( {
     success: function(coll, resp) {
                self.afterRender();
            }
 });

答案 1 :(得分:0)

不是在视图中创建backgrid实例(this.seenPeopleView)而是将实例创建为

var grid = new Backgrid.Grid({...<your columns and collection related code>..});

然后渲染网格并将根目录附加到HTML文档

$('#seen-people-list').append(grid.render().el);

希望它能奏效:)