From 5a322e2c7c23eb1a753992579c0e4c0e4de67eb1 Mon Sep 17 00:00:00 2001 From: Rufus Pollock Date: Sun, 9 Dec 2012 18:58:52 +0000 Subject: [PATCH] [#286,bugfix]: fix for case where fields array has nulls in it. If passed fields = [ null, ..., ...] or [ ... , null, ...] we fail in Dataset._normalizeRecordsAndFields method because: 1. If null is first element we do not do field generation correctly because typeof(null) is object - https://github.com/okfn/recline/blob/ede211646ad6baa82cfb3e53a599b1547abc2c6f/src/model.js#L108 2. We call toString on field names which fails for null https://github.com/okfn/recline/blob/ede211646ad6baa82cfb3e53a599b1547abc2c6f/src/model.js#L113 --- src/model.js | 8 ++++++-- test/model.test.js | 9 +++++---- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/model.js b/src/model.js index 84d0b14f..f5354210 100644 --- a/src/model.js +++ b/src/model.js @@ -105,12 +105,16 @@ my.Dataset = Backbone.Model.extend({ } // fields is an array of strings (i.e. list of field headings/ids) - if (fields && fields.length > 0 && typeof(fields[0]) != 'object') { + if (fields && fields.length > 0 && (fields[0] === null || typeof(fields[0]) != 'object')) { // Rename duplicate fieldIds as each field name needs to be // unique. var seen = {}; fields = _.map(fields, function(field, index) { - field = field.toString(); + if (field === null) { + field = ''; + } else { + field = field.toString(); + } // cannot use trim as not supported by IE7 var fieldId = field.replace(/^\s+|\s+$/g, ''); if (fieldId === '') { diff --git a/test/model.test.js b/test/model.test.js index 8f7dd909..0d227908 100644 --- a/test/model.test.js +++ b/test/model.test.js @@ -180,21 +180,22 @@ test('_normalizeRecordsAndFields', function () { records: null }, }, + // non-string fields { in_: { - fields: [ 1, 1, 3 ], - records: null + fields: [ null, 1, 1, 3 ], + records: [ [1,2,3,4] ] }, exp: { fields: [ + {id: '_noname_'}, {id: '1'}, {id: '11'}, {id: '3'} ], - records: null + records: [ { '_noname_': 1, '1': 2, '11': 3, '3': 4 } ] }, }, - // field is *not* a string // records array but no fields { in_: {