Merge pull request #660 from strongloop/feature/clone-base-property-def

Make sure base property definitions are cloned
This commit is contained in:
Raymond Feng 2015-07-10 10:39:46 -07:00
commit c95e1195df
3 changed files with 24 additions and 1 deletions

View File

@ -498,6 +498,7 @@ DataSource.prototype.setupDataAccess = function (modelClass, settings) {
// Set the default id type from connector's ability // Set the default id type from connector's ability
var idType = this.connector.getDefaultIdType() || String; var idType = this.connector.getDefaultIdType() || String;
idProp.type = idType; idProp.type = idType;
modelClass.definition.rawProperties[idName].type = idType;
modelClass.definition.properties[idName].type = idType; modelClass.definition.properties[idName].type = idType;
if (settings.forceId) { if (settings.forceId) {
modelClass.validatesAbsenceOf(idName, {if: 'isNewRecord'}); modelClass.validatesAbsenceOf(idName, {if: 'isNewRecord'});

View File

@ -377,7 +377,13 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett
continue; continue;
} }
if (subclassProperties[key] === undefined) { if (subclassProperties[key] === undefined) {
subclassProperties[key] = properties[key]; var baseProp = properties[key];
var basePropCopy = baseProp;
if (baseProp && typeof baseProp === 'object') {
// Deep clone the base prop
basePropCopy = mergeSettings(null, baseProp);
}
subclassProperties[key] = basePropCopy;
} }
} }

View File

@ -691,6 +691,22 @@ describe('Load models with base', function () {
Customer.definition.properties.name.should.have.property('type', String); Customer.definition.properties.name.should.have.property('type', String);
}); });
it('should inherit properties by clone from base option', function () {
var ds = new ModelBuilder();
var User = ds.define('User', {name: String});
var Customer1 = ds.define('Customer1', {vip: Boolean}, {base: 'User'});
var Customer2 = ds.define('Customer2', {vip: Boolean}, {base: 'User'});
Customer1.definition.properties.should.have.property('name');
Customer2.definition.properties.should.have.property('name');
Customer1.definition.properties.name.should.not.be.equal(
Customer2.definition.properties.name);
Customer1.definition.properties.name.should.eql(
Customer2.definition.properties.name);
});
it('should revert properties from base model', function() { it('should revert properties from base model', function() {
var ds = new ModelBuilder(); var ds = new ModelBuilder();