loopback-datasource-juggler/lib/list.js

183 lines
3.8 KiB
JavaScript
Raw Normal View History

2012-09-10 15:57:21 +00:00
module.exports = List;
function List(data, type, parent) {
2012-09-11 16:51:31 +00:00
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;
2012-09-10 15:57:21 +00:00
if (typeof type === 'object' && type.constructor.name === 'Array') {
2012-09-11 16:51:31 +00:00
list.ItemType = type[0] || ListItem;
2012-09-10 15:57:21 +00:00
}
2012-09-11 16:51:31 +00:00
data.forEach(function (item, i) {
2012-09-11 19:22:55 +00:00
data[i] = new Item(item, list);
Object.defineProperty(list, data[i].id, {
writable: true,
enumerable: false,
configurable: true,
2012-09-11 19:22:55 +00:00
value: data[i]
});
2012-09-11 16:51:31 +00:00
if (list.nextid <= data[i].id) {
list.nextid = data[i].id + 1;
}
2012-09-10 15:57:21 +00:00
});
2012-09-11 16:51:31 +00:00
Object.defineProperty(list, 'length', {
enumerable: false,
configurable: true,
get: function () {
return list.items.length;
}
});
return list;
2012-09-10 15:57:21 +00:00
}
2012-09-11 19:22:55 +00:00
var _;
try {
_ = require('underscore');
} catch (e) {
_ = false;
}
if (_) {
var _import = [
// collection methods
'each',
'map',
'reduce',
'reduceRight',
'find',
'filter',
'reject',
'all',
'any',
'include',
'invoke',
'pluck',
'max',
'min',
'sortBy',
'groupBy',
'sortedIndex',
'shuffle',
'toArray',
'size',
// array methods
'first',
'initial',
'last',
'rest',
'compact',
'flatten',
'without',
'union',
'intersection',
'difference',
'uniq',
'zip',
'indexOf',
'lastIndexOf',
'range'
];
_import.forEach(function (name) {
List.prototype[name] = function () {
var args = [].slice.call(arguments);
args.unshift(this.items);
return _[name].apply(_, args);
};
});
}
2012-09-10 15:57:21 +00:00
List.prototype.toObject = function () {
return this.items;
};
2012-09-11 19:22:55 +00:00
List.prototype.toJSON = function () {
return this.items;
};
List.prototype.toString = function () {
return JSON.stringify(this.items);
};
2012-09-10 15:57:21 +00:00
List.prototype.autoincrement = function () {
return this.nextid++;
};
List.prototype.push = function (obj) {
var item = new ListItem(obj, this);
this.items.push(item);
return item;
};
List.prototype.remove = function (obj) {
2012-09-11 16:51:31 +00:00
var id = obj.id ? obj.id : obj;
console.log(id);
var found = false;
2012-09-10 15:57:21 +00:00
this.items.forEach(function (o, i) {
2012-09-11 16:51:31 +00:00
if (o.id === id) found = i;
2012-09-10 15:57:21 +00:00
});
2012-09-11 16:51:31 +00:00
if (found !== false) {
delete this[id];
this.items.splice(found, 1);
2012-09-10 15:57:21 +00:00
}
};
List.prototype.forEach = function (cb) {
this.items.forEach(cb);
};
List.prototype.sort = function (cb) {
return this.items.sort(cb);
};
List.prototype.map = function (cb) {
if (typeof cb === 'function') return this.items.map(cb);
if (typeof cb === 'string') return this.items.map(function (el) {
if (typeof el[cb] === 'function') return el[cb]();
if (el.hasOwnProperty(cb)) return el[cb];
});
};
function ListItem(data, parent) {
for (var i in data) this[i] = data[i];
Object.defineProperty(this, 'parent', {
writable: false,
enumerable: false,
configurable: true,
value: parent
});
2012-09-11 19:22:55 +00:00
if (!this.id) {
this.id = parent.autoincrement();
}
if (parent.ItemType) {
this.__proto__ = parent.ItemType.prototype;
if (parent.ItemType !== ListItem) {
parent.ItemType.apply(this);
}
}
this.save = function (c) {
parent.parent.save(c);
};
2012-09-10 15:57:21 +00:00
}