2013-02-08 63 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