From e0f5f45b050343bf6541b33ef23482162e7b6e54 Mon Sep 17 00:00:00 2001 From: Anatoliy Chakkaev Date: Tue, 11 Sep 2012 20:51:31 +0400 Subject: [PATCH] List improvements --- lib/abstract-class.js | 13 ++++++----- lib/list.js | 51 ++++++++++++++++++++++++++++++++++--------- test/common_test.js | 29 +++++++++++++++++++++++- 3 files changed, 77 insertions(+), 16 deletions(-) diff --git a/lib/abstract-class.js b/lib/abstract-class.js index fc98861a..652457dc 100644 --- a/lib/abstract-class.js +++ b/lib/abstract-class.js @@ -66,12 +66,15 @@ AbstractClass.prototype._initProperties = function (data, applySetters) { var type = properties[attr].type; if (BASE_TYPES.indexOf(type.name) === -1) { - try { - this[_attr] = JSON.parse(this[_attr] + ''); - if (type.name === 'Array' || typeof type === 'object' && type.constructor.name === 'Array') { - this[_attr] = new List(this[_attr], type, this); + if (typeof this[_attr] !== 'object') { + try { + this[_attr] = JSON.parse(this[_attr] + ''); + } catch (e) { + console.log(e.stack); } - } catch (e) { + } + if (type.name === 'Array' || typeof type === 'object' && type.constructor.name === 'Array') { + this[_attr] = new List(this[_attr], type, this); } } diff --git a/lib/list.js b/lib/list.js index 7697e7b5..32097c01 100644 --- a/lib/list.js +++ b/lib/list.js @@ -2,18 +2,46 @@ module.exports = List; function List(data, type, parent) { - this.parent = parent; - this.nextid = 1; - data = this.items = data || []; - var Item = this.ItemType = ListItem; + var list = this; + + Object.defineProperty(list, 'parent', { + writable: false, + enumerable: false, + configurable: false, + value: parent + }); + + Object.defineProperty(list, 'nextid', { + writable: true, + enumerable: false, + value: 1 + }); + + data = list.items = data || []; + var Item = list.ItemType = ListItem; if (typeof type === 'object' && type.constructor.name === 'Array') { - this.ItemType = Item = type[0] || ListItem; + list.ItemType = type[0] || ListItem; } - data.forEach(function (item) { + data.forEach(function (item, i) { data[i] = new Item(item, parent); + list[data[i].id] = data[i]; + if (list.nextid <= data[i].id) { + list.nextid = data[i].id + 1; + } }); + + Object.defineProperty(list, 'length', { + enumerable: false, + configurable: true, + get: function () { + return list.items.length; + } + }); + + return list; + } List.prototype.toObject = function () { @@ -35,12 +63,15 @@ List.prototype.push = function (obj) { }; List.prototype.remove = function (obj) { - var found; + var id = obj.id ? obj.id : obj; + console.log(id); + var found = false; this.items.forEach(function (o, i) { - if (o.id === obj.id) found = i; + if (o.id === id) found = i; }); - if (found) { - this.items.splice(i, 1); + if (found !== false) { + delete this[id]; + this.items.splice(found, 1); } }; diff --git a/test/common_test.js b/test/common_test.js index 80f965ed..4ddc7d0a 100644 --- a/test/common_test.js +++ b/test/common_test.js @@ -813,7 +813,34 @@ function testOrm(schema) { test.equal(like.constructor.name, 'ListItem'); var related = post.related.push({hello: 'world'}); test.ok(related.someMethod); - test.done(); + post.save(function (err, p) { + test.equal(p.likes.nextid, 2); + p.likes.push({second: 2}); + p.likes.push({third: 3}); + p.save(function (err) { + Post.find(p.id, function (err, pp) { + test.equal(pp.likes.length, 3); + test.ok(pp.likes[3].third); + test.ok(pp.likes[2].second); + test.ok(pp.likes[1].foo); + pp.likes.remove(2); + test.equal(pp.likes.length, 2); + test.ok(!pp.likes[2]); + pp.likes.remove(pp.likes[1]); + test.equal(pp.likes.length, 1); + test.ok(!pp.likes[1]); + test.ok(pp.likes[3]); + pp.save(function () { + Post.find(p.id, function (err, pp) { + test.equal(pp.likes.length, 1); + test.ok(!pp.likes[1]); + test.ok(pp.likes[3]); + test.done(); + }); + }); + }); + }); + }); }); it('all tests done', function (test) {