Improve Base.readAll() to check entries for arrays and pass them to constructors as arguments rather than their containing array.

This commit is contained in:
Jürg Lehni 2011-05-03 12:06:59 +01:00
parent 49645f8a0d
commit 6b4f142838

View file

@ -55,25 +55,28 @@ this.install = function(scope) {
Base.inject({
statics: true,
read: function(args, start, length) {
read: function(list, start, length) {
var start = start || 0,
length = length || args.length - start;
var arg = args[start];
length = length || list.length - start;
var arg = list[start];
// If the class defines _readNull, return null when nothing was provided
if (arg instanceof this
|| this.prototype._readNull && arg == null && length <= 1)
return arg;
var obj = new this(this.dont);
obj = obj.initialize.apply(obj, start > 0 || length < args.length
? Array.prototype.slice.call(args, start, start + length)
: args) || obj;
obj = obj.initialize.apply(obj, start > 0 || length < list.length
? Array.prototype.slice.call(list, start, start + length)
: list) || obj;
return obj;
},
readAll: function(args, start) {
var res = [];
for (var i = start || 0, l = args.length; i < l; i++)
res.push(this.read(args, i, 1));
readAll: function(list, start) {
var res = [], entry;
for (var i = start || 0, l = list.length; i < l; i++) {
res.push(Array.isArray(entry = list[i])
? this.read(entry, 0)
: this.read(list, i, 1));
}
return res;
},