Browse Source

Added property `length` holding the total number of items in the DataSet

v3_develop
jos 9 years ago
parent
commit
7e8ca2dd07
4 changed files with 41 additions and 0 deletions
  1. +3
    -0
      HISTORY.md
  2. +24
    -0
      docs/dataset.html
  3. +5
    -0
      lib/DataSet.js
  4. +9
    -0
      test/DataSet.test.js

+ 3
- 0
HISTORY.md View File

@ -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

+ 24
- 0
docs/dataset.html View File

@ -21,6 +21,7 @@
<li><a href="#Example">Example</a></li>
<li><a href="#Construction">Construction</a></li>
<li><a href="#Methods">Methods</a></li>
<li><a href="#Properties">Properties</a></li>
<li><a href="#Subscriptions">Subscriptions</a></li>
<li><a href="#Data_Manipulation">Data Manipulation</a></li>
<li><a href="#Data_Selection">Data Selection</a></li>
@ -373,6 +374,29 @@ var data = new vis.DataSet([data] [, options])
</table>
<h2 id="Properties">Properties</h2>
<p>DataSet contains the following properties.</p>
<table>
<colgroup>
<col width="200">
</colgroup>
<tr>
<th>Property</th>
<th>Type</th>
<th>Description</th>
</tr>
<tr>
<td>length</td>
<td>Number</td>
<td>The number of items in the DataSet.</td>
</tr>
</table>
<h2 id="Subscriptions">Subscriptions</h2>
<p>

+ 5
- 0
lib/DataSet.js View File

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

+ 9
- 0
test/DataSet.test.js View File

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

Loading…
Cancel
Save