diff --git a/lib/model.js b/lib/model.js index cf44d678..3c39f2a6 100644 --- a/lib/model.js +++ b/lib/model.js @@ -321,6 +321,7 @@ ModelBaseClass.prototype.toObject = function (onlySchema, removeHidden, removePr var props = Model.definition.properties; var keys = Object.keys(props); var propertyName, val; + for (var i = 0; i < keys.length; i++) { propertyName = keys[i]; val = self[propertyName]; @@ -393,7 +394,7 @@ ModelBaseClass.prototype.toObject = function (onlySchema, removeHidden, removePr if (removeHidden && Model.isHiddenProperty(propertyName)) { continue; } - if (removeHidden && Model.isProtectedProperty(propertyName)) { + if (removeProtected && Model.isProtectedProperty(propertyName)) { continue; } var ownVal = self[propertyName]; @@ -416,11 +417,11 @@ ModelBaseClass.prototype.toObject = function (onlySchema, removeHidden, removePr }; ModelBaseClass.isProtectedProperty = function (propertyName) { - var model = this; + var Model = this; var settings = Model.definition && Model.definition.settings; - var protectedProperties = settings & (settings.protectedProperties || settings.protected); - + var protectedProperties = settings && (settings.protectedProperties || settings.protected); if (Array.isArray(protectedProperties)) { + // Cache the protected properties as an object for quick lookup settings.protectedProperties = {}; for (var i = 0; i < protectedProperties.length; i++) { settings.protectedProperties[protectedProperties[i]] = true; diff --git a/test/model-definition.test.js b/test/model-definition.test.js index c81f518d..9f394f05 100644 --- a/test/model-definition.test.js +++ b/test/model-definition.test.js @@ -271,7 +271,7 @@ describe('ModelDefinition class', function () { }); it('should serialize protected properties into JSON', function() { - var memory = new DataSoruce({connector: Memory}); + var memory = new DataSource({connector: Memory}); var modelBuilder = memory.modelBuilder; var ProtectedModel = memory.createModel('protected', {}, { protected: ['protectedProperty'] @@ -285,7 +285,7 @@ describe('ModelDefinition class', function () { }); }); - it('should not serialized protected properties of nested models into JSON', function(done){ + it('should not serialize protected properties of nested models into JSON', function(done){ var memory = new DataSource({connector: Memory}); var modelBuilder = memory.modelBuilder; var Parent = memory.createModel('parent');