我正在学习backgrid.js的基础知识。因此,当我尝试在主页Backgrid.js上复制代码时,由于传递列的对象数组时的特定错误,我无法呈现网格。我相信我使用的格式正确
var columns = [
{ name: "name", label: "Name", cell: "string" },
{ name: "address", label: "Address", cell: "string" },
{ name: "tel", label: "Phone", cell: "integer" },
{ name: "email", label: "Email", cell: "string" },
{ name: "type", label: "Contact Type", cell: "string" }
];
错误 Uncaught TypeError:Object [object Object]在此步骤初始化网格的过程中没有方法'listenTo':
var grid = new Backgrid.Grid({
columns: columns,
collection: this.collection
});
我是如何初始化网格的?
答案 0 :(得分:2)
问题在于我使用的backbone.js的版本。我推荐使用适当版本的库。
Backgrid.js依赖于3个库来运行:
jquery> = 1.7.0,underscore.js~1.4.0,backbone.js~0.9.10。
答案 1 :(得分:0)
这是一个关于如何使用它的简单结构。
HTML
<div id="container"></div>
JS
$(function(){
/** Columns definition */
var columns = [
{ name: "name", label: "Name", cell: "string" },
{ name: "address", label: "Address", cell: "string" },
{ name: "tel", label: "Phone", cell: "integer" },
{ name: "email", label: "Email", cell: "string" },
{ name: "type", label: "Contact Type", cell: "string" }
];
/** Model instance */
var mdl = Backbone.Model.extend({});
/** Collection instance */
var col = Backbone.Collection.extend({
model: mdl
});
/** Test array of JSON (usually coming from a server) */
var json = [
{"name": "Goose", "address": "a 1st address", "tel": 25500100, "email": "w@test.net","type": 1},
{"name": "Ducky", "address": "a 2nd address", "tel": 25500123, "email": "w@test1.net","type": 2}
];
/** Create the Grid */
var grid = new Backgrid.Grid({
columns: columns,
collection: new col(json)
});
/** Add the Grid to the container */
$("#container").append(grid.render().$el);
});