From 6e6905353cc4b858aad2aac92e3604563188f3d1 Mon Sep 17 00:00:00 2001 From: Alex de Mulder Date: Mon, 13 Jan 2014 17:40:32 +0100 Subject: [PATCH] DATASET: Added options flag for including internal ids, added test coverage for it --- src/DataSet.js | 28 +++++++++++++++++++++++++--- test/dataset.js | 18 ++++++++++++++++++ 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/src/DataSet.js b/src/DataSet.js index 36604a58..13e0f52e 100644 --- a/src/DataSet.js +++ b/src/DataSet.js @@ -42,6 +42,7 @@ function DataSet (options) { this.data = {}; // map with data indexed by id this.fieldId = this.options.fieldId || 'id'; // name of the field containing id this.convert = {}; // field types by field name + this.showInternalIds = this.options.showInternalIds || false; // show internal ids with the get function if (this.options.convert) { for (var field in this.options.convert) { @@ -275,6 +276,7 @@ DataSet.prototype.update = function (data, senderId) { */ DataSet.prototype.get = function (args) { var me = this; + var globalShowInternalIds = this.showInternalIds; // parse the arguments var id, ids, options, data; @@ -318,6 +320,13 @@ DataSet.prototype.get = function (args) { type = 'Array'; } + // we allow the setting of this value for a single get request. + if (options != undefined) { + if (options.showInternalIds != undefined) { + this.showInternalIds = options.showInternalIds; + } + } + // build options var convert = options && options.convert || this.options.convert; var filter = options && options.filter; @@ -352,6 +361,9 @@ DataSet.prototype.get = function (args) { } } + // restore the global value of showInternalIds + this.showInternalIds = globalShowInternalIds; + // order the results if (options && options.order && id == undefined) { this._sort(items, options.order); @@ -831,7 +843,7 @@ DataSet.prototype._getItem = function (id, convert) { if (raw.hasOwnProperty(field)) { value = raw[field]; // output all fields, except internal ids - if ((field != fieldId) || !(value in internalIds)) { + if ((field != fieldId) || (!(value in internalIds) || this.showInternalIds)) { converted[field] = util.convert(value, convert[field]); } } @@ -843,13 +855,12 @@ DataSet.prototype._getItem = function (id, convert) { if (raw.hasOwnProperty(field)) { value = raw[field]; // output all fields, except internal ids - if ((field != fieldId) || !(value in internalIds)) { + if ((field != fieldId) || (!(value in internalIds) || this.showInternalIds)) { converted[field] = value; } } } } - return converted; }; @@ -883,6 +894,17 @@ DataSet.prototype._updateItem = function (item) { return id; }; +/** + * check if an id is an internal or external id + * @param id + * @returns {boolean} + * @private + */ +DataSet.prototype.isInternalId = function(id) { + return (id in this.internalIds); +}; + + /** * Get an array with the column names of a Google DataTable * @param {DataTable} dataTable diff --git a/test/dataset.js b/test/dataset.js index 63446918..8784119c 100644 --- a/test/dataset.js +++ b/test/dataset.js @@ -143,5 +143,23 @@ assert.deepEqual(data.getIds({ }), [3,1]); +data.clear(); + + +// test if the setting of the showInternalIds works locally for a single get request +data.add({content: 'Item 1'}); +data.add({content: 'Item 2'}); + +assert.deepEqual((data.get()[0].id == undefined), true); +assert.deepEqual((data.get({"showInternalIds": true})[0].id == undefined),false); +assert.deepEqual(data.isInternalId(data.get({"showInternalIds": true})[0].id), true); +assert.deepEqual((data.get()[0].id == undefined), true); + +// check if the global setting is applied correctly +var data = new DataSet({showInternalIds: true}); +data.add({content: 'Item 1'}); +assert.deepEqual((data.get()[0].id == undefined), false); +assert.deepEqual(data.isInternalId(data.get()[0].id), true); +assert.deepEqual((data.get({"showInternalIds": false})[0].id == undefined),true); // TODO: extensively test DataSet