Skip to content

Commit

Permalink
List improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
1602 committed Sep 11, 2012
1 parent c3af8b2 commit e0f5f45
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 16 deletions.
13 changes: 8 additions & 5 deletions lib/abstract-class.js
Expand Up @@ -66,12 +66,15 @@ AbstractClass.prototype._initProperties = function (data, applySetters) {
var type = properties[attr].type; var type = properties[attr].type;


if (BASE_TYPES.indexOf(type.name) === -1) { if (BASE_TYPES.indexOf(type.name) === -1) {
try { if (typeof this[_attr] !== 'object') {
this[_attr] = JSON.parse(this[_attr] + ''); try {
if (type.name === 'Array' || typeof type === 'object' && type.constructor.name === 'Array') { this[_attr] = JSON.parse(this[_attr] + '');
this[_attr] = new List(this[_attr], type, this); } 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);
} }
} }


Expand Down
51 changes: 41 additions & 10 deletions lib/list.js
Expand Up @@ -2,18 +2,46 @@
module.exports = List; module.exports = List;


function List(data, type, parent) { function List(data, type, parent) {
this.parent = parent; var list = this;
this.nextid = 1;
data = this.items = data || []; Object.defineProperty(list, 'parent', {
var Item = this.ItemType = ListItem; 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') { 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); 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 () { List.prototype.toObject = function () {
Expand All @@ -35,12 +63,15 @@ List.prototype.push = function (obj) {
}; };


List.prototype.remove = 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) { this.items.forEach(function (o, i) {
if (o.id === obj.id) found = i; if (o.id === id) found = i;
}); });
if (found) { if (found !== false) {
this.items.splice(i, 1); delete this[id];
this.items.splice(found, 1);
} }
}; };


Expand Down
29 changes: 28 additions & 1 deletion test/common_test.js
Expand Up @@ -813,7 +813,34 @@ function testOrm(schema) {
test.equal(like.constructor.name, 'ListItem'); test.equal(like.constructor.name, 'ListItem');
var related = post.related.push({hello: 'world'}); var related = post.related.push({hello: 'world'});
test.ok(related.someMethod); 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) { it('all tests done', function (test) {
Expand Down

0 comments on commit e0f5f45

Please sign in to comment.