2013-02-08 65 views
1

我只是在学习关于backgrid.js的基础知识。因此,当我尝试复制主页Backgrid.js上的代码时,由于传递列中对象数组时出现特定错误,我无法渲染网格。我相信我使用正确的格式Backgrid无法呈现列

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" } 
]; 

误差未捕获的类型错误:对象[对象的对象]无方法“listenTo”发生在在此步骤初始化网格的过程:

var grid = new Backgrid.Grid({ 
     columns: columns, 
     collection: this.collection 
    }); 

我是如何初始化网格的问题?

+0

你可以发布你的HTML吗? – 2013-02-08 21:17:13

回答

2

的问题是与我使用Backbone.js的版本。我推荐使用正确版本的库。

Backgrid.js depends on 3 libraries to function:

jquery >= 1.7.0, underscore.js ~ 1.4.0, and backbone.js ~ 0.9.10.

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": "[email protected]","type": 1}, 
     {"name": "Ducky", "address": "a 2nd address", "tel": 25500123, "email": "[email protected]","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); 
}); 
+0

不幸的是,它似乎不是一个格式问题。使用给定的格式。当它尝试调用第1620行的'self.listenTo(column,“change:renderable”,self.renderColumn)''函数时,我在初始化网格时仍然收到相同的错误(上图)。 – DJCrossman 2013-02-11 14:06:00