Proper collection caching

This commit is contained in:
Anatoliy Chakkaev 2012-10-14 15:02:13 +04:00
parent f59cf0ddbf
commit f6d8b08631
2 changed files with 7 additions and 4 deletions

View File

@ -326,14 +326,17 @@ AbstractClass.all = function all(params, cb) {
} }
var constr = this; var constr = this;
this.schema.adapter.all(this.modelName, params, function (err, data) { this.schema.adapter.all(this.modelName, params, function (err, data) {
var collection = null; var collection = null, cache = {};
if (data && data.map) { if (data && data.map) {
collection = new Array(); collection = new Array();
data.forEach(function (d, i) { data.forEach(function (d, i) {
collection.__defineGetter__(i, function () { collection.__defineGetter__(i, function () {
if (cache[i]) {
return cache[i];
}
var obj = new constr; var obj = new constr;
obj._initProperties(d, false); obj._initProperties(d, false);
collection[i] = obj; cache[i] = obj;
return obj; return obj;
}); });
}); });
@ -535,7 +538,7 @@ AbstractClass.prototype.toObject = function (onlySchema) {
}; };
AbstractClass.prototype.hasOwnProperty = function (prop) { AbstractClass.prototype.hasOwnProperty = function (prop) {
return this.__data.hasOwnProperty(prop) || return this.__data && this.__data.hasOwnProperty(prop) ||
Object.getOwnPropertyNames(this).indexOf(prop) !== -1; Object.getOwnPropertyNames(this).indexOf(prop) !== -1;
}; };

View File

@ -20,7 +20,7 @@
}, },
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"test": "EXCEPT=cradle nodeunit test/*_test*" "test": "EXCEPT=cradle,neo4j nodeunit test/*_test*"
}, },
"engines": [ "engines": [
"node >= 0.4.12" "node >= 0.4.12"