List improvements
This commit is contained in:
parent
c3af8b2aea
commit
e0f5f45b05
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
51
lib/list.js
51
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);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue