Skip to content
This repository has been archived by the owner on Apr 2, 2019. It is now read-only.

Add ability to extend Grid with default columns. #445

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/backgrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -2745,6 +2745,9 @@ var Grid = Backgrid.Grid = Backbone.View.extend({
/** @property */
footer: null,

/** @property */
columns: [],

/**
Initializes a Grid instance.

Expand All @@ -2757,6 +2760,10 @@ var Grid = Backgrid.Grid = Backbone.View.extend({
@param {Backgrid.Footer} [options.footer=Backgrid.Footer] An optional Footer class.
*/
initialize: function (options) {

// Set Columns, either passed by options or columns property.
options.columns = options.columns || this.columns;

// Convert the list of column objects here first so the subviews don't have
// to.
if (!(options.columns instanceof Backbone.Collection)) {
Expand Down
7 changes: 7 additions & 0 deletions src/grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ var Grid = Backgrid.Grid = Backbone.View.extend({
/** @property */
footer: null,

/** @property */
columns: [],

/**
Initializes a Grid instance.

Expand All @@ -82,6 +85,10 @@ var Grid = Backgrid.Grid = Backbone.View.extend({
@param {Backgrid.Footer} [options.footer=Backgrid.Footer] An optional Footer class.
*/
initialize: function (options) {

// Set Columns, either passed by options or columns property.
options.columns = options.columns || this.columns;

// Convert the list of column objects here first so the subviews don't have
// to.
if (!(options.columns instanceof Backbone.Collection)) {
Expand Down
36 changes: 36 additions & 0 deletions test/grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ describe("A Grid", function () {

var books;
var grid;
var extendedGrid;

beforeEach(function () {
books = new Books([{
id: 1,
Expand All @@ -34,6 +36,20 @@ describe("A Grid", function () {
collection: books,
footer: Backgrid.Footer
});

extendedGrid = Backgrid.Grid.extend({
columns: [
{
name: 'title',
cell: 'string'
},
{
name: 'id',
cell: 'integer'
}
]
});

});

it("renders a table with a body, optional header, and an optional footer section", function () {
Expand Down Expand Up @@ -140,4 +156,24 @@ describe("A Grid", function () {
expect($(tbody).find("tr:nth-child(3) > td.integer-cell.editable.sortable.renderable").text()).toBe("3");
});

it("will intantiate with extended columns", function(){

var theGrid = new extendedGrid({ collection: books });

expect(theGrid.columns.length).toBe(2);

});

it('extended grid columns will reset', function(){
var theGrid = new extendedGrid({collection: books});
theGrid.columns.reset([
{
name: 'id',
cell: 'integer'
}
]);

expect(theGrid.columns.length).toBe(1);
});

});