diff --git a/HISTORY.md b/HISTORY.md index f84adb5b..54eafdab 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -4,6 +4,9 @@ http://visjs.org ## not yet released, version 3.9.2-SNAPSHOT +### DataSet + +- Added property `length` holding the total number of items in the DataSet. ## 2015-01-16, version 3.9.1 diff --git a/docs/dataset.html b/docs/dataset.html index d6c2bf81..110afc0a 100644 --- a/docs/dataset.html +++ b/docs/dataset.html @@ -21,6 +21,7 @@
DataSet contains the following properties.
+ +Property | +Type | +Description | +
---|---|---|
length | +Number | +The number of items in the DataSet. | +
diff --git a/lib/DataSet.js b/lib/DataSet.js index f1f3b67a..975f40de 100644 --- a/lib/DataSet.js +++ b/lib/DataSet.js @@ -53,6 +53,7 @@ function DataSet (data, options) { this._options = options || {}; this._data = {}; // map with data indexed by id + this.length = 0; // number of items in the DataSet this._fieldId = this._options.fieldId || 'id'; // name of the field containing id this._type = {}; // internal field types (NOTE: this can differ from this._options.type) @@ -737,6 +738,7 @@ DataSet.prototype._remove = function (id) { if (util.isNumber(id) || util.isString(id)) { if (this._data[id]) { delete this._data[id]; + this.length--; return id; } } @@ -744,6 +746,7 @@ DataSet.prototype._remove = function (id) { var itemId = id[this._fieldId]; if (itemId && this._data[itemId]) { delete this._data[itemId]; + this.length--; return itemId; } } @@ -759,6 +762,7 @@ DataSet.prototype.clear = function (senderId) { var ids = Object.keys(this._data); this._data = {}; + this.length = 0; this._trigger('remove', {items: ids}, senderId); @@ -884,6 +888,7 @@ DataSet.prototype._addItem = function (item) { } } this._data[id] = d; + this.length++; return id; }; diff --git a/test/DataSet.test.js b/test/DataSet.test.js index 85f8e455..0def1526 100644 --- a/test/DataSet.test.js +++ b/test/DataSet.test.js @@ -26,6 +26,7 @@ describe('DataSet', function () { ]); var items = data.get(); + assert.equal(data.length, 4); assert.equal(items.length, 4); items.forEach(function (item) { assert.ok(item.start instanceof Date); @@ -76,6 +77,7 @@ describe('DataSet', function () { {id: 3}, {id: 4} ]); + assert.equal(data.length, 3); // add an item data.add({id: 5, content: 'Item 5', start: now.valueOf()}); @@ -87,12 +89,17 @@ describe('DataSet', function () { {id: 4}, {id: 5} ]); + assert.equal(data.length, 4); // update an item data.update({id: 5, content: 'changed!'}); // update item (extend existing fields) + assert.equal(data.length, 4); data.remove(3); // remove existing item + assert.equal(data.length, 3); data.add({id: 3, other: 'bla'}); // add new item + assert.equal(data.length, 4); data.update({id: 6, content: 'created!', start: now.valueOf()}); // this item is not yet existing, create it + assert.equal(data.length, 5); assert.deepEqual(data.get().sort(sort), [ {id: 1, content: 'Item 1', start: now}, {id: 3, other: 'bla'}, @@ -100,8 +107,10 @@ describe('DataSet', function () { {id: 5, content: 'changed!', start: now}, {id: 6, content: 'created!', start: now} ]); + assert.equal(data.length, 5); data.clear(); + assert.equal(data.length, 0); assert.equal(data.get().length, 0);