DJCrossman
DJCrossman

Reputation: 31

Backgrid unable to render columns

I am just learning the basics of backgrid.js. So when I attempt to replicate the code on the main page Backgrid.js, I am unable to render a grid due to a particular error when passing in an array of objects for the columns. I believe I am using proper format

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

The error Uncaught TypeError: Object [object Object] has no method 'listenTo' occurs in the process of initializing the grid at this step:

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

Is there an issue with how I am initializing the grid?

Upvotes: 1

Views: 2242

Answers (2)

DJCrossman
DJCrossman

Reputation: 31

The issue was with the version of backbone.js I was using. I reccommend using the proper version of libraries.

Backgrid.js depends on 3 libraries to function:

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

Upvotes: 2

Dennis Rongo
Dennis Rongo

Reputation: 4611

Here's a simple structure on how to use it.

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);
});

Upvotes: 0

Related Questions