Backgrid 正在渲染
<table class="backgrid"></table>
但仅此而已。未达到 Backgrid:render() 中的断点。我是一个骨干新手,正在改编别人的代码,所以不确定到底应该发生什么,但 LayoutManager:render() 被调用。它似乎永远不会到达后格...我想要显示的数据正在被获取,看起来好像它们格式正确......但不得不承认,一旦它们被包裹在 Backbone 系列中,就很难分辨。任何关于如何调试/为什么 Backgrid 的渲染没有被感激地调用的指针。
代码如下:
听视图.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);
}
在获取的成功方法上,你应该调用 afterRender。
var self=this;
this.model.attributes.seenPeople.fetch( {
success: function(coll, resp) {
self.afterRender();
}
});
而不是在视图中创建 backgrid 实例 (this.seenPeopleView) 创建实例为
var grid = new Backgrid.Grid({...<your columns and collection related code>..});
然后呈现网格并将根附加到 HTML 文档
$('#seen-people-list').append(grid.render().el);
希望它能:)